Hi,
I'm trying to simplify some code by running a loop rather than setting the code to run on each of a number of worksheets within a file. The code works for each worksheet individually and is as follows:
For m = 1 To jctn
Sheets("J1").Select
If Sheets("Input Data").Cells(20 + (m - 1) * 8, 4) = 0 And Sheets("Input Data").Cells(21 + (m - 1) * 8, 5) = 0 And Sheets("Input Data").Cells(22 + (m - 1) * 8, 4) = 0 Then GoTo Finish Else GoTo Junction1
Junction1:
'insert Friday data into total traffic matrices
s = 0
For i = 1 To num_times
For j = (20 + (s * 8)) To (25 + (s * 8))
For k = 3 To 9
Movement_num_in = Sheets("Input Data").Cells(j, k).Value
Movement_num_out_f = Sheets("Friday Output").Cells(i + 1, 5).Value
Quarter_f = Sheets("Friday Output").Cells(i + 1, 2).Value
Total_Flow_f = Sheets("Friday Output").Cells(i + 1, 7).Value
Total_HGV_f = Sheets("Friday Output").Cells(i + 1, 8).Value
If Movement_num_in = Movement_num_out_f And Quarter_f = 1 Then
Cells(j - 14 - (s * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 2 Then
Cells(j - 5 - (s * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 3 Then
Cells(j + 4 - (s * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 4 Then
Cells(j + 13 - (s * 8), k + 3) = Total_Flow_f
End If
'insert data into HGV matrices
If Movement_num_in = Movement_num_out_f And Quarter_f = 1 Then
Cells(j - 14 - (s * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 2 Then
Cells(j - 5 - (s * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 3 Then
Cells(j + 4 - (s * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 4 Then
Cells(j + 13 - (s * 8), k + 14) = Total_HGV_f
End If
'insert Saturday data into total traffic matrices
Movement_num_out_s = Sheets("Saturday Output").Cells(i + 1, 5).Value
Quarter_s = Sheets("Saturday Output").Cells(i + 1, 2).Value
Total_Flow_s = Sheets("Saturday Output").Cells(i + 1, 7).Value
Total_HGV_s = Sheets("Saturday Output").Cells(i + 1, 8).Value
If Movement_num_in = Movement_num_out_s And Quarter_s = 1 Then
Cells(j + 35 - (s * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 2 Then
Cells(j + 44 - (s * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 3 Then
Cells(j + 53 - (s * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 4 Then
Cells(j + 62 - (s * 8), k + 3) = Total_Flow_s
End If
'insert data into HGV matrices
If Movement_num_in = Movement_num_out_s And Quarter_s = 1 Then
Cells(j + 35 - (s * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 2 Then
Cells(j + 44 - (s * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 3 Then
Cells(j + 53 - (s * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 4 Then
Cells(j + 62 - (s * 8), k + 14) = Total_HGV_s
End If
Next k
Next j
Next i
m = m + 1
If m > jctn Then GoTo Finish
This continues with Sheets("J2").Select, and so on for up to 20 different sheets. This code takes data from the first and second sheets in the file and enters it into specified cells; currently, this method of applying the code to each worksheet works, and gives the correct outputs. I've tried to condense the code by using a loop, shown below:
m = 1
S = 0
Sheets("J1").Select
Movements:
Do
If Sheets("Input Data").Cells(26 + (S * 8), 9) = 0 Then GoTo Finish
For i = 1 To num_times
For j = (20 + (S * 8)) To (25 + (S * 8))
For k = 3 To 9
Movement_num_in = Sheets("Input Data").Cells(j, k).Value 'movement number in input sheet
Movement_num_out_f = Sheets("Friday Output").Cells(i + 1, 5).Value 'movement number referenced in Friday Output sheet
Quarter_f = Sheets("Friday Output").Cells(i + 1, 2).Value 'number of quarter-hour segment referenced in Friday Output
Total_Flow_f = Sheets("Friday Output").Cells(i + 1, 7).Value 'Total flow for associated quarter
Total_HGV_f = Sheets("Friday Output").Cells(i + 1, 8).Value 'HGV flow for associated quarter
Movement_num_out_s = Sheets("Saturday Output").Cells(i + 1, 5).Value 'as _f, but Saturday
Quarter_s = Sheets("Saturday Output").Cells(i + 1, 2).Value 'as _f, but Saturday
Total_Flow_s = Sheets("Saturday Output").Cells(i + 1, 7).Value 'as _f, but Saturday
Total_HGV_s = Sheets("Saturday Output").Cells(i + 1, 8).Value 'as _f, but Saturday
If Movement_num_in = Movement_num_out_f And Quarter_f = 1 Then
Cells(j - 14 - (S * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 2 Then
Cells(j - 5 - (S * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 3 Then
Cells(j + 4 - (S * 8), k + 3) = Total_Flow_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 4 Then
Cells(j + 13 - (S * 8), k + 3) = Total_Flow_f
End If
'insert data into HGV matrices
If Movement_num_in = Movement_num_out_f And Quarter_f = 1 Then
Cells(j - 14 - (S * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 2 Then
Cells(j - 5 - (S * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 3 Then
Cells(j + 4 - (S * 8), k + 14) = Total_HGV_f
ElseIf Movement_num_in = Movement_num_out_f And Quarter_f = 4 Then
Cells(j + 13 - (S * 8), k + 14) = Total_HGV_f
End If
'insert Saturday data into total traffic matrices
If Movement_num_in = Movement_num_out_s And Quarter_s = 1 Then
Cells(j + 35 - (S * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 2 Then
Cells(j + 44 - (S * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 3 Then
Cells(j + 53 - (S * 8), k + 3) = Total_Flow_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 4 Then
Cells(j + 62 - (S * 8), k + 3) = Total_Flow_s
End If
'insert data into HGV matrices
If Movement_num_in = Movement_num_out_s And Quarter_s = 1 Then
Cells(j + 35 - (S * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 2 Then
Cells(j + 44 - (S * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 3 Then
Cells(j + 53 - (S * 8), k + 14) = Total_HGV_s
ElseIf Movement_num_in = Movement_num_out_s And Quarter_s = 4 Then
Cells(j + 62 - (S * 8), k + 14) = Total_HGV_s
End If
If ActiveSheet.Index = (Sheets.Count - 1) Then
GoTo Finish
Else: Sheets(ActiveSheet.Index + 2).Activate
End If
Next k
Next j
Next i
S = S + 1
m = m + 1
Loop Until m = jctn
But this gives me blank output. I am fairly new to this, although I did write the original code myself.
Can anyone give me any pointers please?
Bookmarks