#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 61
Default Help with macro

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

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default Help with macro

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

--
HTH...

Jim Thomlinson


"jhyatt" wrote:

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

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 61
Default Help with macro

problem it inserted blank rows all the way down to row 254

"Jim Thomlinson" wrote:

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

--
HTH...

Jim Thomlinson


"jhyatt" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default Help with macro

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
--
HTH...

Jim Thomlinson


"jhyatt" wrote:

problem it inserted blank rows all the way down to row 254

"Jim Thomlinson" wrote:

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

--
HTH...

Jim Thomlinson


"jhyatt" wrote:

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

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 61
Default Help with macro

works great thanks jim

"Jim Thomlinson" wrote:

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
--
HTH...

Jim Thomlinson


"jhyatt" wrote:

problem it inserted blank rows all the way down to row 254

"Jim Thomlinson" wrote:

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

--
HTH...

Jim Thomlinson


"jhyatt" wrote:

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
using a cell value to control a counter inside a macro and displaying macro value ocset Excel Worksheet Functions 1 September 10th 06 05:32 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 1 June 11th 05 12:44 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 0 June 10th 05 03:38 PM


All times are GMT +1. The time now is 12:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"