0

I have a few sheets in a workbook. They are each called Sheet (1), Sheet (2) etc.

I want to run a loop to go through each sheet and highlight a particular cell.

I have tried setting this up in vba, but can't seem to work out how to loop through the sheets.

Dim i As Integer

For i = 1 To 13

Sheets("Sheet (" & i & ")").Columns("L").Delete Shift:=xlShiftToLeft

Next i

However, this doesn't seem to loop through each of the sheets.

Mikku
  • 6,538
  • 3
  • 15
  • 38
Jane90
  • 37
  • 2
  • 3
  • 6
  • Are you trying to delete column `L` from every sheet ? – Mikku Aug 14 '19 at 06:10
  • 2
    Works for me. Though you may want to qualify which workbook the sheets are in, assuming it's `ThisWorkbook`. – BigBen Aug 14 '19 at 06:11
  • Try `Activeworkbook.Sheets("Sheet (" & i & ")").Columns("L").Delete Shift:=xlShiftToLeft` – Mikku Aug 14 '19 at 06:12
  • Thanks all, defining the Activeworkbook helped. – Jane90 Aug 14 '19 at 06:19
  • 2
    @Mikku why use ActiveWorkbook? That's asking for trouble.. – Tim Stack Aug 14 '19 at 06:30
  • I know it's not Ideal, But we don't know where the Macro is Saved, maybe in Personal Workbook or an Addin. Most probable scenario for anyone to run a Macro is after being on that WorkBook. – Mikku Aug 14 '19 at 07:09
  • Have a look at defining workbook names, I use wbtarget & wbmaster... : https://stackoverflow.com/q/50776026/4961700 – Solar Mike Aug 14 '19 at 07:42

1 Answers1

0

You can loop through sheets like below :

Sub LoopSheets()
 
    Dim Sh As Worksheet

    For Each Sh In ActiveWorkbook.Sheets
        Sh.Columns("L").Delete Shift:=xlShiftToLeft
    Next Sh

End Sub