Using a single macro on several tables across several sheets - excel

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

Related

Copy sheet from closed workbook to active workbook sheet with explanation [duplicate]

This question already has an answer here:
Excel VBA to copy and paste as values from a closed workbook to an active workbook, Mac OS X
(1 answer)
Closed 2 years ago.
I know this is probably very basic but I cannot find a consistent code which works.
I am trying to copy the first and only sheet in a closed file on a shared drive location into the first sheet of the active workbook from which I want to run the macro from.
If anyone can please share any help with an explanation of the code of the steps that would be great.
Thank you
One of the most useful features for VBA Excel programmers is the Excel macro recorder. I frequently use the recorder to learn how Excel would write code to implement actions that I take via mouse clicks and menu selections.
Cut to the chase. Suppose my source workbook is called katy_movies.xlsx and contains a list of viewed/favorite movie titles and various cell formatting. Suppose I have that spreadsheet open and want to copy and paste it (with source formatting retained) into a blank worksheet in a different workbook. Here's almost the entire code that the macro recorder "wrote" based on my mouse clicks and menu selections (I made 2 adjustments).
Note: my original solution failed to open the closed source workbook. Here is code that does that and, as a bonus, closes the source after the copy/paste operation:
Sub Macro3()
'
' Macro3 Macro (recorded by excel macro recorder)
'
Workbooks.Open Filename:="\path\to\katy_movies.xlsx"
End Sub
Sub Macro2()
'
' Macro2 Macro
'
Macro3
Windows("katy_movies.xlsx").Activate
Cells.Select
Selection.Copy
Windows("Book1").Activate
Range("A1").Select ' I manually added this line to ensure a valid paste locn
' for an entire worksheet
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Range("A1").Select ' Manually added this line to unselect all cells following
' paste operation.
Range("A1").Copy ' Effectively empty clipboard to stop Excel from whining
' about the next Close operation.
Windows("katy_movies.xlsx").Close ' close the briefly opened spreadsheet
' force focus back to original worksheet
Windows("Book1").Activate
End Sub
I can't stress enough the usefulness of the macro recorder....

Why is defined range not working with End(xlDown).Select?

I am running a function with a defined range and when trying to use End.(xlDown).Select I get a message "Select method of Range class failed".
I have edited the code below to show only the problem piece. After literally two hours of trying everything, I cannot get it to work.
The annoying part is that I have to use defined ranges since the function is part of a much larger Sub that doesn't work as intended once Select and Activate are used.
Function OutputFunction()
Dim rng8 As Range
Set rng8 = ThisWorkbook.Worksheets(5).Range("A2")
rng1.ClearContents 'Works like a charm.
rng2.Copy 'No problem here either.
rng8.End(xlDown).Select 'Fails misserably.
ActiveCell.Offset(0, 13).Select
Range(Selection, Range("N3")).Select
ActiveSheet.Paste
rng2.Copy destination:= rng8.parent.range(rng8.End(xlDown).Offset(0, 13), rng8.parent.Range("N3"))
"After literally two hours of trying everything, I cannot get it to work."
The first rule of Excel Macros: Never, ever, use SELECT in an Excel Macro.
The second rule of Excel Macros: Don't use Select in Excel Macros
The third.....
Try:
Option Explicit
Sub test()
Dim rng8 As Range
'Have in mind that you refer to a sheet based on it s index, NOT with its name!
'If sheets order change you will refer to another sheet
With ThisWorkbook.Worksheets(5)
Set rng8 = .Range("A2")
rng8.Select
.Range(rng8, rng8.End(xlDown)).Select
End With
End Sub
Try to use End(xlDown).Select in my personal macro. First I tested this in the original excel file that I wrote the macro in, and it worked in every step just fine. But the problem occurred when I used it in another file.
After some tests, I changed the .Select with .Activate and it worked. I'm not 100% sure whether we are talking on the same page or not, so tell me if this solved your problem, so I can improve my answer.

Pasting artihmetic mean VBA

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

Display values greater than a specific number

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

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

Resources