I'm pretty new to VBA, so the following question should not be too difficult. But since I spent about one and a half hour about the code, I think I simply lack some basic understanding how it should look like so I thought it's worth posting it here.
What the code is doing is basically copying values from one range in one sheet to another range in another sheet in the same active workbook containing some kind of a schedule.
I get a "run-time error '9' subscript out of range" error message already in the very first run through the loop in the section
'Subject
Morning = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day)
I tried out the code in a blank workbook so the error should not be due to formatting (in case this might have been the cause). But if you need more information about the context etc. I can provide of course more details.
Thank you for your answers!
Patrick
Sub SetData_new()
' SubModule to copy the data from 'Master_Plan' to particular sheets
Dim CurrWS As Long, StartWS As Long, EndWS As Long
StartWS = Sheets("W_1").Index
EndWS = Sheets("L_1").Index
Dim Weeknr As Integer
Weeknr = 5
Dim Day As Long
Dim Morning As Range, Afternoon As Range, Noon As Range
For CurrWS = StartWS To EndWS
'Set updated ranges
Set Morning = Worksheets(CurrWS).Cells(4, 4)
Set Afternoon = Worksheets(CurrWS).Cells(8, 4)
Set Noon = Worksheets(CurrWS).Cells(12, 4)
For Day = 3 To 21 Step 3
'Subject
Morning = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day)
Afternoon = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day)
Noon = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day)
'Category
Morning.Offset(0, 1) = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day + 1)
Afternoon.Offset(0, 1).Value = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day + 1)
Noon.Offset(0, 1).Value = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day + 1)
'Type
Morning.Offset(0, 2) = Worksheets("ExamSchedule").Cells(Weeknr + 2, Day + 2)
Afternoon.Offset(0, 2).Value = Worksheets("ExamSchedule").Cells(Weeknr + 4, Day + 2)
Noon.Offset(0, 2).Value = Worksheets("ExamSchedule").Cells(Weeknr + 6, Day + 2)
'Specifications
Morning.Offset(0, 3) = Worksheets("ExamSchedule").Cells(Weeknr + 3, Day)
Afternoon.Offset(0, 3).Value = Worksheets("ExamSchedule").Cells(Weeknr + 5, Day)
Noon.Offset(0, 3).Value = Worksheets("ExamSchedule").Cells(Weeknr + 7, Day)
'Increment
Morning = Morning.Offset(12, 0)
Afternoon = Afternoon.Offset(12, 0)
Noon = Noon.Offset(12, 0)
Next Day
Wochennummer = Wochennummer + 9
Next CurrWS
Worksheets("ExamSchedule").Activate
End Sub
DIM(otherwise the variable will be aVariant) egCurrWS` and 2) you should useLongrather thanIntegerunless you have a specific need for 16 bit numbers (its faster, VBA does a background conversion to Long anyway).ExamSchedule? - watch out for spelling, spaces etc