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
Related
Let's say I have two Excel Workbooks(in reality I have one sheet results and maybe a hundered other workbooks containing data). I would like to create a macro that allows me to take the arithmetic mean of a selection and paste that into my active cell. I have written a macro that allows me to paste copied values between different workbooks, really simple:
Sub PasteVal()
Selection.PasteSpecial xlPasteValues
End Sub
Trying to do the arithmetic mean copying does not work, however:
Sub PasteMean()
ActiveCell.PasteSpecial (Application.WorksheetFunction.Average(Selection))
End Sub
Any help would be appreciated
Thanks.
did you try ?
activecell.value=Application.WorksheetFunction.Average(Selection)
casually stumbled upon this post. consider the xlPasteSpecial method with
XlPasteSpecialOperation Enumeration.
xlPasteSpecialOperationAdd
xlPasteSpecialOperationDivide
xlPasteSpecialOperationMultiply
xlPasteSpecialOperationNone
xlPasteSpecialOperationSubtract
With Worksheets("Sheet1")
.Range("C1:C5").Copy
.Range("D1:D5").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With
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
i have the following code in an add-in, which is called from the ribbon. It is meant to make all lines visible from the current line to the next visible line. The lines are hidden by an autofilter.
Sub Details4(control As IRibbonControl)
ActiveSheet.Unprotect Password:="xxx"
SendKeys "+{DOWN}+ ", True
ActiveCell.Rows("1:" & Selection.Rows.Count).EntireRow.EntireRow.AutoFit
End Sub
It does mark the lines as i wanted it to do, but the auto-fit does not work. When I double-click the line-height (which should do the same as the autp-fit) alle lines are visible as I wished this do to.
Can anyone see a problem? I know "sendkey" is not the best Thing in programming, but I had no other easy idea how to mark the range up to the next visible line...
Thanks
max
You have an extra .EntireRow call.
Rows("9:13").EntireRow.autofit
Works just fine.
For your sendkey issue, try something like this:
Range(Selection, Selection.End(xlDown)).Select
solved this by changing the code to:
For Each Row In ActiveCell.Rows("1:" & Selection.Rows.Count)
Row.EntireRow.AutoFit
Next
and deleting the line
ActiveCell.Rows("1:" & Selection.Rows.Count).EntireRow.EntireRow.AutoFit
of course...
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
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.. ;)