Why does Excel VBA not delete rows?

84 views Asked by At

I'm trying to delete excess rows from a sheet prior to printing and the following code does not work:

  ' Should be done!
  Rows(firstRowTemplate + numHits & ":" & lastRow).Select   'get rid of excess rows
  Selection.Delete Shift:=xlUp
  '
  "next line of code"

When I run the code straight through, the rows do not get deleted. If I stop execution before the Selection.Delete line, then step through it, the lines are not deleted. However, if I stop execution at "next line of code" and then reset the execution pointer to the Selection.Delete command, it works. The values in firstRowTemplate, numHits, and lastRow show up correctly. It's as if there is some latency between the .Select and the .Delete. Any thoughts?

Excel Office 365 (Version 2311 Build 16.0.17029.20028) 64-bit Windows 11: Edition Windows 11 Home Version 22H2 Installed on ‎12/‎8/‎2022 OS build 22621.2861 Experience Windows Feature Experience Pack 1000.22681.1000.0

0

There are 0 answers