宏只在一个 sheet 中工作,而不在其他中工作,尽管代码中有限定范围

Macro only working in one sheet and not the others despite qualifying ranges in the code

我在“输出”选项卡中为这个宏分配了一个按钮,效果很好。当我将它复制到其他选项卡时,它会在“Worksheets("Output").Range("B5"...... " 行。

据我所知,我已经从 Worksheets("Output") 开始限定了 sheet。此外,在代码的后面(如果我排除这些行)我的宏从各种 sheets 开始移动,当我用 Sheets("name of sheet") 代码限定它们时就好了。

此外,根据我的研究,我的代码似乎并不完全在 sheet.

Sub test()

Dim i As Integer
Dim Rows As Integer
Dim Count As Integer
Dim StartTime As Double
Dim SecondsElapsed As Double

StartTime = Timer   'Remember time when macro starts
Application.ScreenUpdating = False


Count = WorksheetFunction.CountA(Sheets("Input").Range("B:B")) - 2 'Subtracts (2) to exclude two headers above data


'Copies Name and latest rates into Columns AG and AH for troubleshooting against any new re-runs

Worksheets("Output").Range("B5", Range("B5").End(xlDown)).Copy
Worksheets("Output").Range("AG5").PasteSpecial xlPasteValuesAndNumberFormats

Sheets("Output").Range("T5", Range("T5").End(xlDown)).Copy
Sheets("Output").Range("AH5").PasteSpecial xlPasteValuesAndNumberFormats
    
Sheets("Output").Range("A6").Activate
Range(ActiveCell, ActiveCell.End(xlDown).End(xlToRight)).Clear

有时 With 块对于为每个 Range() 调用添加 sheet 引用很有用:

    With ThisWorkbook.Worksheets("Output")
        .Range("B5", .Range("B5").End(xlDown)).Copy
        .Range("AG5").PasteSpecial xlPasteValuesAndNumberFormats
    
        .Range("T5", .Range("T5").End(xlDown)).Copy
        .Range("AH5").PasteSpecial xlPasteValuesAndNumberFormats
        
        .Range(.Range("A6"), .Range("A6").End(xlDown).End(xlToRight)).Clear
    End With