For Loop through sheets in a workbook – sheet names equal to a cell range
I am trying to write a script to copy and paste a range of data from one workbook to another.
My code currently without the loop and when a single sheet is copied.
I am looking for some guidance on the For Loop portion (first time ever using one).
The sheets “names” are just a range of numbers in which the code will loop through.
Sheet 1 = 1, sheet 2 = 2 …. Sheet 31 = 31
I want the number of loops, to be ran, specified by specific cell values.
For example
If cell “B3” = 4 and cell “C3” = 15
I would like the code to run a for loop for
Sheets 4 through sheet 15.
My 2 questions are:
How do I insert my code into a For loop / which kind of For loop to use?
&
How do I use Sheet( ).select where the inside of the parenthesis is equal to a cell value. (Bold in the code below)
Sub refresh()
Windows("Truck Racks RawData.xlsm").Activate
Sheets("Refresh Data").Select
Dim X As Integer
For X = Range("B3") To Range("C3")
Windows("Truck Log-East Gate-January.xlsx").Activate
Sheets(**"X"**).Select
Sheets(**"X"**).Range("A4:R4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Truck Racks RawData.xlsm").Activate
Sheets("RawDataMacro").Select
Range("A" & Rows.Count).End(xlUp).Select ' starts from the bottom of the worksheet and finds the last cell with data
ActiveCell.Offset(1).Select ' moves cursor down one cell
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next X
End Sub
question from:
https://stackoverflow.com/questions/65904473/for-loop-through-sheets-in-a-workbook-sheet-names-equal-to-a-cell-range-exce 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…