Help with macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My goal is to add a cell between a name i enter and the total so there is
always a blank row. i have this but it only works if cell a4 is changed.

A B c
1 Customers
1 George
2 Sam
3
4 Total

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, "a").End(xlUp).Row
If Intersect(Target, Range("A4")) Is Nothing Then
Exit Sub
Else
'The cell you are monitoring is changed!
For i = lastrow To 2 Step -1
If InStr(1, Cells(i, "a"), "Total", vbTextCompare) Then
Rows(i).Insert
End If
Next
Application.EnableEvents = True
End If
End Sub

Any help would be appreciated
 
Using your existing code this should work...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, "a").End(xlUp).Row
If Target.Column = 1 Then
'The cell you are monitoring is changed!
For i = lastrow To 2 Step -1
If InStr(1, Cells(i, "a"), "Total", vbTextCompare) Then
Rows(i).Insert
End If
Next
Application.EnableEvents = True
End If
End Sub
 
Your code never diables events. It re-nables but it never diabled in the
first place... Try this...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, "a").End(xlUp).Row
If Target.Column = 1 Then
Application.EnableEvents = False
'The cell you are monitoring is changed!
For i = lastrow To 2 Step -1
If InStr(1, Cells(i, "a"), "Total", vbTextCompare) Then
Rows(i).Insert
End If
Next
Application.EnableEvents = True
End If
End Sub
 
works great thanks jim

Jim Thomlinson said:
Your code never diables events. It re-nables but it never diabled in the
first place... Try this...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, "a").End(xlUp).Row
If Target.Column = 1 Then
Application.EnableEvents = False
'The cell you are monitoring is changed!
For i = lastrow To 2 Step -1
If InStr(1, Cells(i, "a"), "Total", vbTextCompare) Then
Rows(i).Insert
End If
Next
Application.EnableEvents = True
End If
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

Similar Threads

HELP Copy And Paste 7
automatic page break 1
Run two macros for two different sheets 11
Macro range 2
R1C1 1
Macro Question 3
After Macro runs, no cell activated - what am I missing? 2
Macro-indirect cell 4

Back
Top