Display values greater than a specific number - excel

I have a report in Excel which lists clients and the amount of hours that we work on them.
I want to create a separate sheet within this workbook that pulls the clients that we work on over 40 hours, through VBA.
For example basic version of my report looks like this in Sheet 1
Client------Client ID---Hours
Client 1------1947--------30
Client 2------6465--------46
Client 3------8787--------20
Client 4------7878--------15
Client 5------4873--------48
I want my new sheet to display
Client------Client ID---Hours
Client 2------6465--------46
Client 5------4873--------48
I am wondering if it is a while loop but wouldn't it break as soon as it finds the first value greater than 40 then it wouldn't continue to the next set of values?

I recorded a quick macro to get the code for the filter criteria and then used Copy Destination:= . Recording macros and reviewing them is a great way to start learning some VBA
Sub Shorty()
Cells.Select
Selection.AutoFilter
ActiveSheet.UsedRange.AutoFilter Field:=3, Criteria1:=">40", _
Operator:=xlAnd
ActiveSheet.UsedRange.Copy Destination:=ActiveWorkbook.Sheets("Sheet2").Range("A1")
Selection.AutoFilter
End Sub
You can find the documentation for the Copy Method here

Related

Need to convert VBA code to Excel online Scripting

Good morning all,
Can someone help me by converting this simple VBA code to Excel Script code?
What the script does is it selects and entire row from the active cell and pastes it on the first row to the Template sheet.
Sub Copy()
ActiveCell.EntireRow.Select
Selection.Copy
Sheets("Template").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Instead of all the copying and pasting, why not simply use this piece of code (obviously you need to run this while having a worksheet open, different than the "Template" one):
Worksheets("Template").Range("1:1").Value = ActiveSheet.Range("1:1").Value
More information on this can be found in this other StackOverflow post.

Excel: Copy Row to New Tab if Cell Contains Value

I'm a complete novice when it comes to Excel, I've Googled it and it's come up with some responses that I don't understand in the slightest. Most of which are things I need to do in Visual Basic?
Essentially what I want is to set up several tabs for individual users, that generate from the main tab. All of which are set up already.
So IF Row R on Main Sheet has NO (Persons Initials)
Copy this row to the Nick tab (There will be 20+ rows for each user, not sure if that changes anything)
Any help would be incredible and massively appreciated!
Thanks,
Nick
You can probably get a start by recording a macro. Here's a link on how to do that: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-macro-HP010342374.aspx#BMrecordmacro. After you click record, just start doing what you want done manually. That will show you some of the syntax. In this case, it will probably be easiest for you to format your summary data as a table, filter it by each person's initials and then copy the results to the individual tabs. Your recorded code is probably going to look something like this:
Sub CopyNO()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Range("Table1[#All]").Select
Selection.Copy
Sheets("NO").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
To clean up the code you record, you'll want to try to change anything that says ActiveSheet to something like Sheets("SheetName") and, ideally, get rid of any .Select commands by instead using whatever is actually done with the selection. Here you might end up with something like:
Sub CopyNO()
Sheets("Summary").ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:= _
"NO"
Worksheets("Summary").Range("Table1").Copy _
Destination:=Worksheets("NO").Range("A5")
End Sub

Using a single macro on several tables across several sheets

I have a workbook that is split one page=one month of the year.
on each of those sheets is a table with various conditional formatting on it.
from there I filter the information several ways to obtain certain bits of information, (outstanding balances, items waiting for dispatch etc etc)
I am trying to set up some simple macros to make filtering quicker, e.g show only orders from a particular company, that have been dispatched, but not paid for.
My problem is, the macro only applies to the sheet/table I created it on, is it possible to reword the vb code to make the macro work across all sheets/tables of my work book?
This is an example of one of the macros, sorry if there is protocol for inserting code on this forum, I plead ignorance.
Sub HSWC_only()
'
' HSWC_only Macro
' leaves only hi-spec jobs
'
' Keyboard Shortcut: Ctrl+m
'
ActiveSheet.ListObjects("Table25").Range.AutoFilter Field:=3, Criteria1:= _
"=HSWC*", Operator:=xlAnd
End Sub
It looks like your macro should do that since it refers to ActiveSheet, so it should work on any sheet you're currently in.
If it doesn't it's because of the ListObjects("Table25").Range reference - You might want to change that to something along the lines of UsedRange
Your code would look something along the lines of:
ActiveSheet.UsedRange.AutoFilter Field:=3, Criteria1:= _
"=HSWC*", Operator:=xlAnd
Without seeing your sheets / layout, that's the closest I can offer to a solution.
Hope this helps.
The key you are looking for is the ActiveSheet, this now applies the Autofilter only to the sheet you see in front of you. If you would change that you an object that you can link to each sheet you could apply it to each sheet in order.
Now when your sheets all have only one List you could reference the 1st ListObject in stead of its name.
If you would put the line in a Loop like this:
Sub HSWC_only()
'
'HSWC_only Macro
' leaves only hi-spec jobs
'
' Keyboard Shortcut: Ctrl+m
'
Dim ws as Worksheet
For each ws in ThisWorkbook.Worksheets
ws.ListObjects(1).Range.AutoFilter Field:=3, Criteria1:= _
"=HSWC*", Operator:=xlAnd
Next ws
End Sub

Excel Filtering and Copying in VBA

I'm working on a VBA script that pulls a range of dates from Access, then filters the data and creates a chart based on the filtered data. The filtered data will be going to a separate sheet where the chart will be pulling its data from . I can get data out of Access with a SQL statement, but my AutoFilter in Excel is erroring out. Here is what I have...
Sheet3.Range("F4:F500").AutoFilter(, "Riveter 01").Copy Destination:=Sheet2.Range("A5")
It gives an app-defined or object-defined error and I can't figure out why. Is this the proper way or is there an easier way?
PS: This filter will happen with 22 unique machines so I was planning on running a loop for each machine. If that is not the fastest or proper way please let me know.
You need to split this into two parts. Filter and then copy/ paste. See below:
With Sheet3
.AutoFilterMode = False
With .Range("F4:F500")
.AutoFilter Field:=1, Criteria1:="Riveter 01"
.SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A5")
End With
End With
to remove the filter:
On Error Resume Next
Sheet3.ShowAllData
On Error GoTo 0
On Error Resume Next is for when there is no filter present to skip the error. Please note the use of Sheet3 and Sheet2 for those looking for a generic solution.
I think that you have to do this in 2 separate steps:
filter the data
copy the filtered data to another sheet
The answer here has an excellent example of how to do this: Autofilter Macro, then copy visible data ONLY and paste to next available row

Run Time Error '1004': Select method of Range Class failed VBA 2003

I am trying to copy a column from one sheet to another. The code I am using is a recorded macro and it works fine until I connect it to a button. When I do so, it gives a
Run Time Error '1004': Select method of Range Class failed
Here is the code and I can see nothing wrong with it. When I hit debug it highlights the second line.
Sheets("Count").Select
Columns("C:C").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("b1").Select
ActiveSheet.Paste
Sheets("Count").Select
Sheets("Count").Columns("A:A").Select
Columns("A:A").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("A1").Select
ActiveSheet.Paste
I have no clue what the problem is. Please help
You should always avoid using .Select They are a major cause of errors. You may want to see How to avoid using Select in Excel VBA
Sub Sample()
Sheets("Count").Columns("C:C").Copy _
Sheets("Add Invintory").Columns("B:B")
Sheets("Count").Columns("A:A").Copy _
Sheets("Add Invintory").Columns("A:A")
End Sub
I think the issue is that you have written the code in another sheet's code module. If I'm in Sheet1, and write e.g.
Sheets("Sheet2").Select
Columns("A:A").Select
...then Excel assumes you are referring to the Columns on Sheet1 as it treats the current sheet as a default. Therefore, you've told Excel "select Sheet 2" then "select a column on Sheet 1"...which it can't do so it gives you an error message. The best solution would be not to use 'Select'...but you will still see in Siddharth's code that he has had to refer to sheet addresses explicitly
Your original code would have worked if placed in the ThisWorkbook module. Locations for entering code are explained towards the end of this Excel help video
When you are putting vba code into the "view sheet code" .. There definitely helps to use Application.Run ... to run macro..
I had problem i directly input macro to sheet code.. for selection in another sheet it claimed runtime error 1004.. So i created macro separately and then i put Application.Run my macro from sheet code.
Works out perfectly ;)
This Application.Run also helps when you have too big macro that excel claim it cant be so big. You can easily divide to several parts and then just run applications one by one.. ;)

Resources