See code below:
This code opens other excel sheets based on the path file name being in the J column. It then opens the sheets and add the text HOURS to the next available D field on the "Work" sheet based on the value in C2 of the "Hours' sheet. It works but I get a 400 error when the task is completed because the code doesn't know to stop not to mention it's just really sloppy and long winded to begin with. Anyone know how to condense this with a loop?
Code:
Sub runallmacros()
Dim wbSource As Workbook
Dim wsDest As Worksheet
Dim row As Integer, os As Integer
Set wsDest = ThisWorkbook.Worksheets("File Copier")
row = 2
os = 0
With wsDest
Do While .Range("K" & row).Value <> ""
Set wbSource = Workbooks.Open(wsDest.Range("K" & row).Value)
wbSource.Worksheets(1).Range("A5:D500").Copy
Application.DisplayAlerts = False
wbSource.Close
Application.DisplayAlerts = True
If .Range("A1").Value <> "" Then os = 1
.Range("A" & .Rows.Count).End(xlUp).Offset(os).PasteSpecial
row = row + 1
Loop
End With
Set wbSource = Nothing
End Sub
Sub runallmacros2()
Procedure1
Procedure2
Procedure3
Procedure4
Procedure5
Procedure6
Procedure7
Procedure8
Procedure9
Procedure10
Procedure11
Procedure12
Procedure13
Procedure14
Procedure15
Procedure16
Procedure17
Procedure18
End Sub
Sub Procedure1()
Dim wb As Workbook, sh As Worksheet
Set wb = Workbooks.Open(Range("J2").Value)
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "1" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "Hours"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "2" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "3" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "4" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "5" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "6" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "7" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "8" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "9" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
Set sh = wb.Sheets("Hours")
If sh.Range("c2") = "10" Then
Set sh = wb.Sheets("Work")
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
sh.Range("D" & Rows.Count).End(xlUp).Offset(1).Value = "HOURS"
End If
wb.Save
wb.Close
End Sub
This code opens other excel sheets based on the path file name being in the J column. It then opens the sheets and add the text HOURS to the next available D field on the "Work" sheet based on the value in C2 of the "Hours' sheet. It works but I get a 400 error when the task is completed because the code doesn't know to stop not to mention it's just really sloppy and long winded to begin with. Anyone know how to condense this with a loop?
Last edited: