reuben_rambo
New Member
- Joined
- Mar 13, 2023
- Messages
- 6
- Office Version
- 365
- 2021
- 2019
- Platform
- Windows
Hi all:
I'm hoping that one of the amazing VBA macro gurus can help me. I have two VBA programs that work, but can't seem to combine them.
I save all my VBA programs in PERSONAL.xlsmb
Would anyone be able to help combine both the VBA programs placed here?
I look forward to get some help (if possible). Thanks!
Best,
Reuben
[1] VBA program (saves all sheets from an excel workbook to CSV) - IT WORKS! (after alot of testing)
Sub ExportSheetsToCSV()
Dim xWs As Worksheet
Dim xcsvFile As String
Dim originalFileName As String
originalFileName = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Activate
xcsvFile = ActiveWorkbook.Path & "\" & originalFileName & "_" & xWs.Name & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=xcsvFile, _
FileFormat:=xlCSV, CreateBackup:=False
Next
End Sub
[2] VBA program (Loops through all excel files in a given folder) ... IT WORKS! .... obtained from Loop Through All Excel Files In A Given Folder
This VBA macro will let you select a folder path and loop through each of the Excel files in the folder. The following macro code
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
I'm hoping that one of the amazing VBA macro gurus can help me. I have two VBA programs that work, but can't seem to combine them.
I save all my VBA programs in PERSONAL.xlsmb
Would anyone be able to help combine both the VBA programs placed here?
I look forward to get some help (if possible). Thanks!
Best,
Reuben
[1] VBA program (saves all sheets from an excel workbook to CSV) - IT WORKS! (after alot of testing)
Sub ExportSheetsToCSV()
Dim xWs As Worksheet
Dim xcsvFile As String
Dim originalFileName As String
originalFileName = ActiveWorkbook.Path & "\" & Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Activate
xcsvFile = ActiveWorkbook.Path & "\" & originalFileName & "_" & xWs.Name & ".csv"
Application.ActiveWorkbook.SaveAs Filename:=xcsvFile, _
FileFormat:=xlCSV, CreateBackup:=False
Next
End Sub
[2] VBA program (Loops through all excel files in a given folder) ... IT WORKS! .... obtained from Loop Through All Excel Files In A Given Folder
This VBA macro will let you select a folder path and loop through each of the Excel files in the folder. The following macro code
- Opens each Excel file within the user-selected folder
- Performs a task
- Saves the file
- Closes the workbook
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub