遍历单元格范围 B3-B21 中的工作簿名称

Loop Through Workbooks names in Cell range B3-B21

我有 vba 代码可以在工作簿中打开 sheet 并将特定范围复制到相同的 sheet。下面的代码,我如何创建一个循环,而不是一遍又一遍地复制代码来更改单元格引用。而是在 B3 副本中打开 sheet,打开 b4 副本等直到 B27?我环顾四周,但可能是我使用的搜索词目前找不到解决方案,

Worksheets(Worksheets("Contents").Range("B3").Value).Activate Range("B2").Select

转到下一个 sheet 我会手动将 B3 更改为 b4,我如何创建一个循环来为定义的 Bx 范围执行此操作?

下面正在执行完整代码

`Worksheets(Worksheets("Contents").Range("B3").Value).Activate
Range("B2").Select
Count rows in raw data

intRowsused = ActiveSheet.UsedRange.Rows.Count 'count rows used in the table

'Copy

Range("B3:T" & intRowsused).Copy

Sheets("Master Template").Select

Set rng = ActiveSheet.Range("A:T")

    lastRow = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

writeRow = lastRow + 1

Range("A" & writeRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False `

任何帮助将不胜感激。 谢谢

您可以编辑代码以包含 "For" 循环。我只想用 "B & i".

替换你对 B3 的所有引用
dim i as long
for i = 3 to 27
'edit code to include "i" instead of 3, 4 , 5 through 27

Range("B" & i & ":T" & intRowsused).Copy

Next i

这是避免使用 select

的方法
Option Explicit 
Sub CopyToMaster()

    Dim wb As Workbook
    Dim wsContents As Worksheet, wsSource As Worksheet, wsTarget As Worksheet
    Dim rngSource As Range, rngTarget As Range, cell As Range
    Dim targetRow As Long

    Set wb = ActiveWorkbook ' or thisworkbook
    Set wsTarget = wb.Sheets("Master Template")
    Set wsContents = wb.Sheets("Contents")

    targetRow = wsTarget.Cells(Rows.Count, 1).End(xlUp).Row + 1

    For Each cell In wsContents.Range("B3:B5")
        Set wsSource = wb.Sheets(cell.Value)

        Set rngSource = wsSource.Range("B3:T" & wsSource.UsedRange.Rows.Count)
        rngSource.Copy

        Set rngTarget = wsTarget.Range("A" & targetRow)
        rngTarget.PasteSpecial Paste:=xlPasteValues

        targetRow = targetRow + rngSource.Rows.Count
    Next

    MsgBox "Copy complete", vbInformation
End Sub