0

This is my VBA code below, it executes when running with F8 but when running it with F5. it doesn't give any error or anything, it just doesn't run.

Dim LastColumn As Long
Set ws = ActiveSheet
Dim LastRow As Long
LastRow = 0
LastColumn = 0

LastRow = ws.Range("c" & Rows.Count).End(xlUp).Row
LastColumn = ws.Cells(11, ws.Columns.Count).End(xlToLeft).Column
Range(Cells(12, 1), Cells(LastRow, LastColumn)).Select
Selection.SpecialCells(xlCellTypeVisible).Interior.ColorIndex = -4142
delay 60
Range("A1").Select

I tried to delay the code for 60 seconds, maybe it runs too fast but doesn't work either. any help?

  • 3
    How do you know it doesn't run? Did you try using `Debug.Print` or a `MsgBox` to try to debug it? Side note: In general, you want to [avoid using Select](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) in your code – cybernetic.nomad Mar 20 '23 at 14:38
  • Have you included all of your code? e.g., Is this in a `Sub`? – Marc Mar 20 '23 at 14:44

1 Answers1

0

Here is a version of your code with the ranges qualified back to Worksheet level:

Dim LastColumn As Long
Set ws = ActiveSheet ' you should change this to Worksheets("YourSheetName") to be safer
Dim LastRow As Long
LastRow = 0
LastColumn = 0
With ws
    LastRow = .Range("c" & .Rows.Count).End(xlUp).Row
    LastColumn = .Cells(11, .Columns.Count).End(xlToLeft).Column
    .Range(.Cells(12, 1), .Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = -4142
    delay 60
    .Range("A1").Select
End With

This is assuming that all the ranges in question are on the same Activesheet.

CLR
  • 11,284
  • 1
  • 11
  • 29