Hello,
I want to run a macro for all .xlsx files in Environ("UserProfile") & "\Desktop\orders\files\
I want it to open the workbook, run my VBA scripts, close the workbook, and loop through every .xlsx file in the folder.
I found a VBA script that can do this, but I have mutiple Subs, so I don't know how to make it work.
Here is the marco I found:
Here are the scripts I want to run on each .xlsx file in the folder, I know most of it could go into one Sub, but I pieced it together over time.
Any help would be appreciated!
I want to run a macro for all .xlsx files in Environ("UserProfile") & "\Desktop\orders\files\
I want it to open the workbook, run my VBA scripts, close the workbook, and loop through every .xlsx file in the folder.
I found a VBA script that can do this, but I have mutiple Subs, so I don't know how to make it work.
Here is the marco I found:
Rich (BB code):
Sub OpenAllWorkbooks()
'Step 1:Declare your variables
Dim MyFiles As String
'Step 2: Specify a target folder/directory, you may change it.
MyFiles = Dir("c:\users\us\desktop\orders\temp\*.xlsx")
Do While MyFiles <> ""
'Step 3: Open Workbooks one by one
Workbooks.Open "c:\users\us\desktop\orders\temp\" & MyFiles
'run some code here
ActiveWorkbook.Close SaveChanges:=False
'Step 4: Next File in the folder/Directory
MyFiles = Dir
Loop
Here are the scripts I want to run on each .xlsx file in the folder, I know most of it could go into one Sub, but I pieced it together over time.
Any help would be appreciated!
Rich (BB code):
Sub SplitOrder()
CopyCurrentSheetToTwoDifferentSheets
Split1Formula
Split2Formula
Paste_OnlyValues
Paste_OnlyValues2
PasteFormat
SumColumnF_1
SumColumnF_2
ChangeDate
SaveActiveSheet_as_Woorkbook1
SaveActiveSheet_as_Woorkbook2
End Sub
Private Sub CopyCurrentSheetToTwoDifferentSheets()
'
Dim CopiesCounter As Long
Dim TotalCopies As Long
Dim xName As String
Dim ws As Worksheet
'
Application.ScreenUpdating = False
'
Set ws = ActiveSheet ' <--- Set this to the sheet that you want to copy
'
On Error Resume Next
'
Application.DisplayAlerts = False
Sheets("S1").Delete
Sheets("S2").Delete
Application.DisplayAlerts = True
'
ws.Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = "S1"
'
ActiveSheet.Cells.UnMerge
Range("G1").EntireColumn.Insert
ws.Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Name = "S2"
'
ActiveSheet.Cells.UnMerge
Range("G1").EntireColumn.Insert
Application.ScreenUpdating = True
End Sub
Private Sub Split1Formula()
Worksheets("Split 1").Range("G25").FormulaR1C1 = "=IF(R[-2]C[-3]>22,""Yes"",0)"
Worksheets("Split 1").Range("G25:G834").FillDown
End Sub
Private Sub Split2Formula()
Worksheets("S2").Range("G25").FormulaR1C1 = "=IF(R[-2]C[-3]>22,""Yes"",0)"
Worksheets("S2").Range("G25:G834").FillDown
End Sub
Private Sub Paste_OnlyValues()
Worksheets("S1").Range("G25:G834").Copy
Worksheets("S1").Range("F25").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Worksheets("S1").Select
Columns("G").Delete
End Sub
Private Sub Paste_OnlyValues2()
Worksheets("S2").Range("G25:G834").Copy
Worksheets("S2").Range("F25").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Worksheets("S2").Select
Columns("G").Delete
End Sub
Private Sub PasteFormat()
Worksheets(1).Select
Cells.Select
Selection.Copy
Sheets("S1").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Sheets("S2").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Private Sub SumColumnF_1()
Dim Lr As Long, i As Long
Lr = Range("E" & Rows.Count).End(xlUp).row
For i = 25 To Lr
If Range("E" & i).HasFormula() = True Then Range("E" & i).AutoFill Destination:=Range("E" & i & ":F" & i)
Next i
End Sub
Private Sub SumColumnF_2()
Sheets("S1").Select
Dim Lr As Long, i As Long
Lr = Range("E" & Rows.Count).End(xlUp).row
For i = 25 To Lr
If Range("E" & i).HasFormula() = True Then Range("E" & i).AutoFill Destination:=Range("E" & i & ":F" & i)
Next i
End Sub
Private Sub ChangeDate()
Sheets("S2").Select
Range("H7").Select
ActiveCell.FormulaR1C1 = "11/1/2021"
End Sub
Private Sub SaveActiveSheet_as_Woorkbook1()
Dim wb As Workbook
Worksheets("S1").Copy
Set wb = ActiveWorkbook
wb.SaveAs Environ("UserProfile") & "\Desktop\orders\S1\" & "1 Order " & Range("B5").Value, FileFormat:=xlOpenXMLWorkbook
wb.Close
End Sub
Private Sub SaveActiveSheet_as_Woorkbook2()
Dim wb As Workbook
Worksheets("S2").Copy
Set wb = ActiveWorkbook
wb.SaveAs Environ("UserProfile") & "\Desktop\orders\S2\" & "2Order " & Range("B5").Value, FileFormat:=xlOpenXMLWorkbook
wb.Close
End Sub