ForrestGump01
New Member
- Joined
- Mar 15, 2019
- Messages
- 23
- Office Version
- 365
- Platform
- Windows
Hi All,
I'm a novice VBA/Macro user, currently seeking some help with writing a dynamic macro.
I've got a process where I have to export a tab of data to a new workbook and save to a specific drive folder. OK, I've got the code for this written, and it works well (below). However, I need make the export dynamic based on whether certain columns are empty, and based on their dates...
The data is a budget reconciliation between two source tabs. In some instances we may need to reconcile past months of actual data, as well as forecast months going forward. However, historical and forecast data needs to be saved as separate files. Right now, my code saves the whole reconciliation as one file. I need to add in some code to separate the data based on if it is before the current month (say prior to July 2019) or current month and beyond (say July 2019 +). How can I add in this criteria and save as two separate exports? I'm not opposed to the criteria being tied to date "input" cells, where the user would manually identify the date ranges for each export, if this is easier than excel calculating the date ranges.
Here is my existing code:
Sub Generate_Buddie()
Dim newWB As Workbook, currentWB As Workbook
Dim newS As Worksheet, currentS As Worksheet
'Copy the data you need
Set currentWB = ThisWorkbook
Sheets("In-School Buddie").Select
Range("A:AS").Select
Selection.Copy
'Create a new file that will receive the data
Set newWB = Workbooks.Add
With newWB
Set newS = newWB.Sheets("Sheet1")
newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
'Save in CSV
Application.DisplayAlerts = False
.SaveAs Filename:="H:\FACT Q3 - Consumer Finance Student\In-School Buddie", FileFormat:=xlCSV
Application.DisplayAlerts = True
End With
End Sub
I'm a novice VBA/Macro user, currently seeking some help with writing a dynamic macro.
I've got a process where I have to export a tab of data to a new workbook and save to a specific drive folder. OK, I've got the code for this written, and it works well (below). However, I need make the export dynamic based on whether certain columns are empty, and based on their dates...
The data is a budget reconciliation between two source tabs. In some instances we may need to reconcile past months of actual data, as well as forecast months going forward. However, historical and forecast data needs to be saved as separate files. Right now, my code saves the whole reconciliation as one file. I need to add in some code to separate the data based on if it is before the current month (say prior to July 2019) or current month and beyond (say July 2019 +). How can I add in this criteria and save as two separate exports? I'm not opposed to the criteria being tied to date "input" cells, where the user would manually identify the date ranges for each export, if this is easier than excel calculating the date ranges.
Here is my existing code:
Sub Generate_Buddie()
Dim newWB As Workbook, currentWB As Workbook
Dim newS As Worksheet, currentS As Worksheet
'Copy the data you need
Set currentWB = ThisWorkbook
Sheets("In-School Buddie").Select
Range("A:AS").Select
Selection.Copy
'Create a new file that will receive the data
Set newWB = Workbooks.Add
With newWB
Set newS = newWB.Sheets("Sheet1")
newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
'Save in CSV
Application.DisplayAlerts = False
.SaveAs Filename:="H:\FACT Q3 - Consumer Finance Student\In-School Buddie", FileFormat:=xlCSV
Application.DisplayAlerts = True
End With
End Sub