Need help with printing VBA Macro

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
Hello,

I need help with a printing macro. I have 4 sheets labeled "FINRA PV" and "FINRA Download" along with its corresponding months, and would like to automatically print all of them.

However, I change the name of one them every month. Currently this is what I have:

Code:
 Sheets("FINRA Jul PV").Select
    Range("F19").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Sheets("FINRA Jul Download").Select
    Range("R10").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Sheets("FINRA Jun PV").Select
    Range("F16").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Sheets("FINRA Jun Download").Select
    Range("I11").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False

Which would print my "July" and "June" tabs. But next month it wont work because I will be re-naming the sheets to "July" and "August".

How do I incorporate using the Sheet "number" instead of the title? The sheet numbers displayed in VBA are Sheets 10,11,15, and 16.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,
you could check for the current month you are in, translate the month number to a value (e.g. 1 = Jan, 2 = Feb)... and then loop through all the sheets and if the month's name is in the Sheet name then print it.
For example.. In the month of August you could print any sheet that has 'Aug' in the sheet name. This would apply to a sheet named "FINRA Aug PV" and also "FINRA Aug Download".
This way you can keep using simple names for the sheets and print only the current month, prior month or which ever month you need to print.

Additionally, you could add any other standard names if there is a sheet you print every month.
 
Upvote 0
How do I incorporate using the Sheet "number" instead of the title? The sheet numbers displayed in VBA are Sheets 10,11,15, and 16.

how about this? (you can choose to comment out or uncomment any ONE of the opening IF statements, according to your needs)

Code:
Option Explicit
Sub print_sheet_acc_to_month()

Dim strMonths As Variant
Dim curMonth As Integer
Dim Ws As Worksheet

curMonth = Month(Now)
strMonths = Array("none", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

For Each Ws In Worksheets

[COLOR="#008000"]    ' If InStr(Ws.Name, strMonths(curMonth)) Then   ' << this will find the sheet with current month
    ' If InStr(Ws.Name, strMonths(curMonth)) Or InStr(Ws.Name, "FINRA") Then  ' << this will find sheets for this month or any including "FINRA"[/COLOR]
   
 If InStr(Ws.Name, strMonths(curMonth)) Or InStr(Ws.Name, strMonths(curMonth - 1)) Then [COLOR="#008000"]' << this will will find sheets for this month and last month[/COLOR]        
        MsgBox Ws.Name [COLOR="#008000"]' use your print sheet code here instead of alerting[/COLOR]
        
    End If
    
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,021
Members
449,281
Latest member
redwine77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top