Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting Rows Without Selecting Anything?

Tags:

excel

vba

I'm working in VBA and want to insert a row in a specific location without selecting it. The issue I'm having is that after the row is selected, the spreadsheet is scrolled down to that row when the script is finished running. I want to be able to do this without the spreadsheet being scrolled down to the inserted row.

Rows(i & ":" & i).Select
ActiveCell.EntireRow.Insert

I don't want to select A1 to get to the top.

like image 684
sooprise Avatar asked Apr 26 '10 15:04

sooprise


2 Answers

Just do this:

Cells(i,1).EntireRow.Insert

Apply the action directly to the desired range, instead of selecting it first and applying the action to "Activecell".

A few additional notes:

  • The "record a macro" feature generates code like your example, but I find that selecting the cell in advance is seldom necessary when writing your own code.
  • A side benefit: operating on the cell directly instead of selecting it first can speed up your code by a factor of 10x to 100x !!
like image 135
BradC Avatar answered Oct 02 '22 01:10

BradC


Rows(i & ":" & i).Insert will insert a row below without changing the selection.

like image 31
Alex K. Avatar answered Oct 02 '22 02:10

Alex K.