why does the execution jump to another procedure?


H

Harold Good

Hi, I have the following code. As I push F8 to execute manually, after the Insert row, it jumps to another procedure, skips around some of those lines of code, then comes back to execute the last line?

I have noticed this pattern in some other spreadsheet VBA as well.

What am I doing wrong? Thanks, Harold


Private Sub InsertRow()
Dim myRow As Long

myRow = ActiveCell.Row
Do Until Cells(myRow, 2) <> Cells(myRow + 1, 2)
myRow = myRow + 1
Loop
Rows(myRow).Insert
Rows(ActiveCell.Row).Select

End Sub
 
Ad

Advertisements

S

slarbie

In the VBA project window, double click on the worksheet object to see if
there is a "worksheet_change" sub. Every time you make a change to the
worksheet this will execute. In the code below, the insert action qualifies
as a worksheet change event.

If you can determine that the change event code is not necessary to current
intended functioning of the file, then you can just delete it. Otherwise,
you'll want to be sure that any new routines you add will work with it in a
predictable way.
 
P

Patrick Molloy

if there is a sheet change event, you can disable it using using the
Application.EnableEvents method ..



Private Sub InsertRow()
Dim myRow As Long

Application.EnableEvents = False

myRow = ActiveCell.Row
Do Until Cells(myRow, 2) <> Cells(myRow + 1, 2)
myRow = myRow + 1
Loop
Rows(myRow).Insert
Rows(ActiveCell.Row).Select

Application.EnableEvents = True

End Sub
 
H

Harold Good

Thank you very much, I had no idea why this was happening on some of my
spreadsheets!
Harold
 
Ad

Advertisements

H

Harold Good

Thank you, very helpful!
Harold


Patrick Molloy said:
if there is a sheet change event, you can disable it using using the
Application.EnableEvents method ..



Private Sub InsertRow()
Dim myRow As Long

Application.EnableEvents = False

myRow = ActiveCell.Row
Do Until Cells(myRow, 2) <> Cells(myRow + 1, 2)
myRow = myRow + 1
Loop
Rows(myRow).Insert
Rows(ActiveCell.Row).Select

Application.EnableEvents = True

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top