I am trying to run a macro through all worksheets in an Excel workbook. I have the code below, but it loops through only the first worksheet. The macro runs in the first worksheet again and again, instead of going on to the next worksheet like it should. Could someone please help? Below is my VBA code.
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