我試圖在Excel工作簿貫穿所有工作表的宏。我有下面的代碼,但它只循環通過第一個工作表。宏一次又一次地在第一個工作表中運行,而不是像它應該進入下一個工作表。有人可以幫忙嗎?以下是我的VBA代碼。遍歷一個Excel工作表的所有宏工作簿
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
' Insert your code here.
'lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Range("P4").Select
ActiveCell.FormulaR1C1 = "=RC[-10]&"" ""&RC[-5]"
Range("P4").Select
Selection.AutoFill Destination:=Range("P4:P65536"), Type:=xlFillDefault
Range("P4:P500").Select
ActiveWindow.SmallScroll Down:=-24
Selection.Copy
Range("R4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveSheet.Range("$R4:$R500").RemoveDuplicates Columns:=1, Header:=xlNo
Selection.TextToColumns Destination:=Range("R4"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
Range("U4").Select
ActiveCell.FormulaR1C1 = "=INDEX(C[-16],MATCH(RC[-3],C[-15],0))"
Range("V4").Select
ActiveCell.FormulaR1C1 = "=INDEX(C[-12],MATCH(RC[-3],C[-11],0))"
Range("U4:V4").Select
Selection.AutoFill Destination:=Range("U4:V41"), Type:=xlFillDefault
Range("U4:V500").Select
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
'MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
Exit Sub
End Sub
你從來沒有真正使用循環變量「我」;代碼應該如何知道你想引用每個連續的表單?問題是你的代碼根本沒有指定一個表單 - 所以它假定你想在活動表單上工作。 –