I'm trying to loop a bunch of code through all worksheets in a workbook. However it is only applying to the sheet I am on when I trigger the macro. I think that this may be caused by the fact that I am activating cells (which I understand is a no-no but don't fully understand how to avoid) ultimately however I am unsure of the cause.
The code I am running is entered below. It is only one part of a larger macro. the parts which follow this are working fine but do not necessitate looping through all sheets and inputting values into them. I'm relatively new to this, so any advice that might be helpful would be very welcome.
Dim ws As Worksheet
For Each ws In Worksheets
' Input Lookup values into Column S
Range("S19").Select
ActiveCell.Formula = "search term"
Range("S20").Select
ActiveCell.Formula = "search term"
Range("S21").Select
ActiveCell.Formula = "search term"
Range("S22").Select
ActiveCell.Formula = "search term"
Range("S23").Select
ActiveCell.Formula = " search term"
Range("S24").Select
ActiveCell.Formula = "search term"
Range("S25").Select
ActiveCell.Formula = "Seach term"
Range("S26").Select
ActiveCell.Formula = "search term"
Range("S27").Select
ActiveCell.Formula = " Search Term"
Range("S28").Select
ActiveCell.Formula = "search term"
Range("S29").Select
ActiveCell.Formula = "search term"
Range("S34").Select
ActiveCell.Formula = "search term"
Range("S35").Select
ActiveCell.Formula = "search term"
' Input Lookup Values into Column V
Range("V19").Select
ActiveCell.Formula = " search termf"
Range("V20").Select
ActiveCell.Formula = " search termes"
Range("V21").Select
ActiveCell.Formula = " search terms"
Range("V22").Select
ActiveCell.Formula = " search termn"
Range("V23").Select
ActiveCell.Formula = " search terme"
Range("V24").Select
ActiveCell.Formula = " search termt"
Range("V25").Select
ActiveCell.Formula = " search terms"
Range("V26").Select
ActiveCell.Formula = " search terms"
Range("V27").Select
ActiveCell.Formula = " search termg"
Range("V28").Select
ActiveCell.Formula = " search termy"
Range("V29").Select
ActiveCell.Formula = " search term"
Range("V34").Select
ActiveCell.Formula = "search term"
Range("V35").Select
ActiveCell.Formula = "search term"
'Input Column T Formulas
Range("T19").Select
ActiveCell.Formula2 = _
"=CELL(""address"",INDEX(R18C13:R95C13,MATCH(RC[-1],R18C3:R95C3,0)))"
Range("T19").Select
Selection.AutoFill Destination:=Range("T19:T36"), Type:=xlFillDefault
' Input Column U Formulas
Range("U19").Select
ActiveCell.Formula2R1C1 = "=CELL(""address"",OFFSET(INDIRECT(RC[-1]),-2,0))"
Range("U19").Select
Selection.AutoFill Destination:=Range("U19:U36"), Type:=xlFillDefault
' Input Column W Formulas
Range("W19").Select
ActiveCell.Formula2R1C1 = _
"=CELL(""address"",INDEX(R18C13:R95C13,MATCH(RC[-1],R18C3:R95C3,0)))"
Range("W19").Select
Selection.AutoFill Destination:=Range("W19:W36"), Type:=xlFillDefault
' Input Column X Formulas
Range("X19").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-3],"":"",RC[-1])"
Range("X19").Select
Selection.AutoFill Destination:=Range("X19:X36"), Type:=xlFillDefault
' Input Dark Box around Ranges and Formulas
Range("S18:X37").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
' Input Formulas into Columns N & O
Range("N18").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(RC[-2]),"""",IFERROR(RC[-2]-RC[-1],RC[-1]))"
Range("N18").Select
Selection.AutoFill Destination:=Range("N18:N260"), Type:=xlFillDefault
Range("N18:N260").Select
Range("O18").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISBLANK(RC[-3]),"""",IFERROR(RC[-1]/RC[-3]*100,RC[-2]))"
Range("O18").Select
Selection.AutoFill Destination:=Range("O18:O260"), Type:=xlFillDefault
Range("O18:O260").Select
Range("L18:L260").Select
Selection.Copy
Range("N18:O260").Select
Range("N260").Activate
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'
'Inputs Column M Formulas
TSW:
If IsError(Range("X19")) Then GoTo TDES
Columns("C:C").EntireColumn.Select
On Error GoTo TDES
Selection.Find(What:="search term", After:=ActiveCell, LookIn _
:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 10).Range("A1").Select
ActiveCell.Formula = "=SUM(INDIRECT(R19C24))"
ActiveCell.Select
TDES:
If IsError(Range("X20")) Then GoTo TOSC
Columns("C:C").EntireColumn.Select
On Error GoTo TOSC
Selection.Find(What:=" search term", After:=ActiveCell, LookIn _
:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 10).Range("A1").Select
ActiveCell.Formula = "=SUM(INDIRECT(R20C24))"
ActiveCell.Select
TOSC
' here my code basically repeats 25 times querying a new generic search term each time, and inputting formulas appropriately.
Next
If there is any way to loop this, I would be very happy to learn of it.