Help needed with VBA code

jrudolph

New Member
Joined
Apr 25, 2008
Messages
35
The following code is used to split a report. What it does is selects the criteria, copies it, and then pastes the data into a new workbook and then saves it using the convention I set. I have the below code copied close to 40 times and the list will probably grow so...

What I am looking to do is come up with better code than the macro I built that will allow me to also add or subtract out "criteria" without needing to find, (to delete out), or copy and edit the existing code to add new criteria. I am also hoping there is a way from within the spreadsheet or even from a pop up box I would be able to change the "month" so that the nameing convention works. I currently open the VBA editor and use find replace to change the month.

Code:
Range("A7:Q3000").Select
 
Selection.AutoFilter Field:=1, Criteria1:="AAA"
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:= _
"U:\May 2008\Avg Inc Report - AAA - May08.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Range("A1").Select
 
Selection.AutoFilter Field:=1, Criteria1:="AAB"
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:= _
"U:\May 2008\Avg Inc Report - AAB - May08.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Range("A1").Select

And So on...


I hope this all make scence. Any help would be greatly appreciated. Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Yes, use an Input Box on prompt...incorporate something like the below into your script

Code:
Sub ....()

Dim I_Date As Date
Dim er As Integer

10
er = 0
I1 = InputBox("Enter Date to Process", "Date", "01-MMM-YY")
On Error GoTo InvalidDate
I_Date = CDate(I1)
If er = 1 Then GoTo 10

....

Exit Sub

InvalidDate:
er = 1
Resume Next

End Sub

you can then use your Input Date and format accordingly in your file paths...

e.g

Code:
"U:\" & Format(I_Date, "MMM YYYY") & "\Avg Inc Report - AAA - " & Format(I_Date, "MMMYY") & ".xls"
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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