Loop through a set number of sheets starting with inputted initial sheet name

Declanscully

New Member
Joined
Mar 5, 2013
Messages
12
I am trying to set the following piece of code to be able to loop through a set of sheets starting with Date1 (inputted from active worksheet), and continuing for the duration of the Result. An example would be Date1: 03-03-2022, Result: 8, For statement would start on the workbook at 03-03-2022 and copy the date for the next 8 sheets.


VBA Code:
Date1 = Worksheets("sheet1").Range("B2").value
Date2 = Worksheets("sheet1").Range("B3").value
Result = DateDiff("D", Date1, Date2)

For i = Date1 To Result 'Starts workbook search after rate sheets
        lr = lr + 1 'sets start row as 5
        With ThisWorkbook.Worksheets("Sheet1").Range("A" & lr)    '<----- Change as required
            .value = cStr(wb2.Worksheets(i).Name)
            .Offset(, 1).value = wb2.Worksheets(i).Range("J61").MergeArea.value
            .Offset(, 2).value = wb2.Worksheets(i).Range("J27").MergeArea.value
            .Offset(, 4).value = wb2.Worksheets(i).Range("J39").MergeArea.value
            .Offset(, 6).value = wb2.Worksheets(i).Range("J50").MergeArea.value
            .Offset(, 7).value = wb2.Worksheets(i).Range("J60").MergeArea.value
            .Offset(, 8).value = wb2.Worksheets(i).Range("B15").MergeArea.value
            .Offset(, 9).value = wb2.Worksheets(i).Range("B16").MergeArea.value
            .Offset(, 10).value = wb2.Worksheets(i).Range("B17").MergeArea.value
            .Offset(, 11).value = wb2.Worksheets(i).Range("B18").MergeArea.value
            .Offset(, 12).value = wb2.Worksheets(i).Range("B19").MergeArea.value
            .Offset(, 13).value = wb2.Worksheets(i).Range("B20").MergeArea.value
            
            End With
    Next i

 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
the loop starts from 0 because it's more convenient to work with indexes starting from 0. The Format(Date1 + i, "dd-mm-yyyy") expression generates the date string for the current sheet, based on the start date (Date1) and the current index (i). Finally, the wb2.Worksheets("Date" & (i + 1)) expression select the worksheet with the name "DateX", where X is the current index plus 1

VBA Code:
Date1 = Worksheets("Sheet1").Range("B2").Value
Date2 = Worksheets("Sheet1").Range("B3").Value
Result = DateDiff("D", Date1, Date2)

For i = 0 To Result - 1 'Starts workbook search after rate sheets
    lr = lr + 1 'sets start row as 5
    With ThisWorkbook.Worksheets("Sheet1").Range("A" & lr) '<----- Change as required
        .Value = Format(Date1 + i, "dd-mm-yyyy")
        .Offset(, 1).Value = wb2.Worksheets("Date" & (i + 1)).Range("J61").MergeArea.Value
        .Offset(, 2).Value = wb2.Worksheets("Date" & (i + 1)).Range("J27").MergeArea.Value
        .Offset(, 4).Value = wb2.Worksheets("Date" & (i + 1)).Range("J39").MergeArea.Value
        .Offset(, 6).Value = wb2.Worksheets("Date" & (i + 1)).Range("J50").MergeArea.Value
        .Offset(, 7).Value = wb2.Worksheets("Date" & (i + 1)).Range("J60").MergeArea.Value
        .Offset(, 8).Value = wb2.Worksheets("Date" & (i + 1)).Range("B15").MergeArea.Value
        .Offset(, 9).Value = wb2.Worksheets("Date" & (i + 1)).Range("B16").MergeArea.Value
        .Offset(, 10).Value = wb2.Worksheets("Date" & (i + 1)).Range("B17").MergeArea.Value
        .Offset(, 11).Value = wb2.Worksheets("Date" & (i + 1)).Range("B18").MergeArea.Value
        .Offset(, 12).Value = wb2.Worksheets("Date" & (i + 1)).Range("B19").MergeArea.Value
        .Offset(, 13).Value = wb2.Worksheets("Date" & (i + 1)).Range("B20").MergeArea.Value
    End With
Next i
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top