Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default VBA to acsertain if last w/s change was "insert Row"

2003/2007

What VBA can I use to trigger additional code if the last action was "Insert Row?"

OR

How to inhibit execution of Worksheet_Change() unless the last w/s change was insert row.

NOTE: I wish that the code be in the worksheet object module (so as to use worksheet events as the
trigger)

Notice the IF clause below. It is this code line that I need clarified. How do I capture
rows.count when the w/s is activated then measure rows.count after row-insertion. If I can, then
the code below will work fine. The issue is how to extend the life of the variable "OrigRows" from
Worksheet_Activate event so that I compare to the current rows.count after worksheet_Change event.

Is there a better way? i.e., VBA-read the last action (via ReDo list?) to trigger the If Clause
below?

'Private Sub Worksheet_Change(ByVal Target As Range)

If Sheets("Trial Balance Current").Rows.Count OrigRows Then
Dim myRange As Range
ActiveSheet.Unprotect Password:="xxxxx"
Set myRange = Intersect(ActiveSheet.UsedRange, Selection)
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect Password:="xxxxx", DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
AllowInsertingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If

***********

The code I could not get to work effectively (preserve the life of the variable).


Function OrigRows() As Long
OrigRows = Sheets("Trial Balance Current").UsedRange.Rows.Count
End Function

Private Sub Worksheet_Activate()

' How do I preseve OrigRows in the function above to use in the code above?

End Sub


Any thoughts appreciated, EagleOne
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default VBA to acsertain if last w/s change was "insert Row"

Take a look at:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Rows.Count = 1 Then
If Target.Columns.Count = Columns.Count Then
MsgBox (" a row just might have been added")
End If
End If
End Sub

It is possible to keep track of the number of rows in ActiveSheet.UsedRange
by using a static variable (declare it just before the sub rather than after
the sub).

However, if the user inserts a cell and pushes the others down, then the
number of rows in UsedRange might grow even though a new row had not been
added.

Consider using all three tests.
--
Gary''s Student - gsnu2007L


" wrote:

2003/2007

What VBA can I use to trigger additional code if the last action was "Insert Row?"

OR

How to inhibit execution of Worksheet_Change() unless the last w/s change was insert row.

NOTE: I wish that the code be in the worksheet object module (so as to use worksheet events as the
trigger)

Notice the IF clause below. It is this code line that I need clarified. How do I capture
rows.count when the w/s is activated then measure rows.count after row-insertion. If I can, then
the code below will work fine. The issue is how to extend the life of the variable "OrigRows" from
Worksheet_Activate event so that I compare to the current rows.count after worksheet_Change event.

Is there a better way? i.e., VBA-read the last action (via ReDo list?) to trigger the If Clause
below?

'Private Sub Worksheet_Change(ByVal Target As Range)

If Sheets("Trial Balance Current").Rows.Count OrigRows Then
Dim myRange As Range
ActiveSheet.Unprotect Password:="xxxxx"
Set myRange = Intersect(ActiveSheet.UsedRange, Selection)
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect Password:="xxxxx", DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
AllowInsertingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If

***********

The code I could not get to work effectively (preserve the life of the variable).


Function OrigRows() As Long
OrigRows = Sheets("Trial Balance Current").UsedRange.Rows.Count
End Function

Private Sub Worksheet_Activate()

' How do I preseve OrigRows in the function above to use in the code above?

End Sub


Any thoughts appreciated, EagleOne

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default VBA to acsertain if last w/s change was "insert Row"

Gary's Student.

Finally, I woke up the giants.

Much appreciated!! Please look at my code submitted after I asked the question.
For my learning process what are its problems if any?


Gary''s Student wrote:

Take a look at:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Rows.Count = 1 Then
If Target.Columns.Count = Columns.Count Then
MsgBox (" a row just might have been added")
End If
End If
End Sub

It is possible to keep track of the number of rows in ActiveSheet.UsedRange
by using a static variable (declare it just before the sub rather than after
the sub).

However, if the user inserts a cell and pushes the others down, then the
number of rows in UsedRange might grow even though a new row had not been
added.

Consider using all three tests.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default VBA to acsertain if last w/s change was "insert Row"

As you have already noticed, variables are usually NOT preserved. However,
if you Dim the variable above the Sub statement, they will be preserved.

For example:

Dim OldRowCount As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(OldRowCount) Then
OldRowCount = ActiveSheet.UsedRange.Rows.Count
Else
MsgBox ("there are " & ActiveSheet.UsedRange.Rows.Count & " rows")
MsgBox ("there were " & OldRowCount & " rows")
OldRowCount = ActiveSheet.UsedRange.Rows.Count
End If
End Sub

1. notice the Sub follows the Dim (opposite of the usual case)
2. we don't need a function, just a variable
3. because OldRowCount is a memory variable, it won't have a value the very
first time the sub is called, so I test for this.
4. you could also have Dim'ed the variable by itself in a standard module
--
Gary''s Student - gsnu200828


" wrote:

Gary's Student.

Finally, I woke up the giants.

Much appreciated!! Please look at my code submitted after I asked the question.
For my learning process what are its problems if any?


Gary''s Student wrote:

Take a look at:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Rows.Count = 1 Then
If Target.Columns.Count = Columns.Count Then
MsgBox (" a row just might have been added")
End If
End If
End Sub

It is possible to keep track of the number of rows in ActiveSheet.UsedRange
by using a static variable (declare it just before the sub rather than after
the sub).

However, if the user inserts a cell and pushes the others down, then the
number of rows in UsedRange might grow even though a new row had not been
added.

Consider using all three tests.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default VBA to acsertain if last w/s change was "insert Row"

You do an excellent job!

Thanks for your time and knowledge.

Gary''s Student wrote:

As you have already noticed, variables are usually NOT preserved. However,
if you Dim the variable above the Sub statement, they will be preserved.

For example:

Dim OldRowCount As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(OldRowCount) Then
OldRowCount = ActiveSheet.UsedRange.Rows.Count
Else
MsgBox ("there are " & ActiveSheet.UsedRange.Rows.Count & " rows")
MsgBox ("there were " & OldRowCount & " rows")
OldRowCount = ActiveSheet.UsedRange.Rows.Count
End If
End Sub

1. notice the Sub follows the Dim (opposite of the usual case)
2. we don't need a function, just a variable
3. because OldRowCount is a memory variable, it won't have a value the very
first time the sub is called, so I test for this.
4. you could also have Dim'ed the variable by itself in a standard module



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default VBA to acsertain if last w/s change was "insert Row"

Thanks for the feedback.
--
Gary''s Student - gsnu200828


" wrote:

You do an excellent job!

Thanks for your time and knowledge.

Gary''s Student wrote:

As you have already noticed, variables are usually NOT preserved. However,
if you Dim the variable above the Sub statement, they will be preserved.

For example:

Dim OldRowCount As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(OldRowCount) Then
OldRowCount = ActiveSheet.UsedRange.Rows.Count
Else
MsgBox ("there are " & ActiveSheet.UsedRange.Rows.Count & " rows")
MsgBox ("there were " & OldRowCount & " rows")
OldRowCount = ActiveSheet.UsedRange.Rows.Count
End If
End Sub

1. notice the Sub follows the Dim (opposite of the usual case)
2. we don't need a function, just a variable
3. because OldRowCount is a memory variable, it won't have a value the very
first time the sub is called, so I test for this.
4. you could also have Dim'ed the variable by itself in a standard module


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default VBA to acsertain if last w/s change was "insert Row"

See if this works for you

Private NumRows As Long

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H1:H10" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

With Target

If Me.UsedRange.Rows.Count NumRows Then

'your code

NumRows = Me.UsedRange.Rows.Count
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
NumRows = Me.UsedRange.Rows.Count
End Sub


--
__________________________________
HTH

Bob

wrote in message
...
2003/2007

What VBA can I use to trigger additional code if the last action was
"Insert Row?"

OR

How to inhibit execution of Worksheet_Change() unless the last w/s change
was insert row.

NOTE: I wish that the code be in the worksheet object module (so as to use
worksheet events as the
trigger)

Notice the IF clause below. It is this code line that I need clarified.
How do I capture
rows.count when the w/s is activated then measure rows.count after
row-insertion. If I can, then
the code below will work fine. The issue is how to extend the life of the
variable "OrigRows" from
Worksheet_Activate event so that I compare to the current rows.count after
worksheet_Change event.

Is there a better way? i.e., VBA-read the last action (via ReDo list?) to
trigger the If Clause
below?

'Private Sub Worksheet_Change(ByVal Target As Range)

If Sheets("Trial Balance Current").Rows.Count OrigRows Then
Dim myRange As Range
ActiveSheet.Unprotect Password:="xxxxx"
Set myRange = Intersect(ActiveSheet.UsedRange, Selection)
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect Password:="xxxxx", DrawingObjects:=False,
Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True,
AllowFormattingRows:=True, _
AllowInsertingRows:=True, AllowSorting:=True,
AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If

***********

The code I could not get to work effectively (preserve the life of the
variable).


Function OrigRows() As Long
OrigRows = Sheets("Trial Balance Current").UsedRange.Rows.Count
End Function

Private Sub Worksheet_Activate()

' How do I preseve OrigRows in the function above to use in the code
above?

End Sub


Any thoughts appreciated, EagleOne



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default VBA to acsertain if last w/s change was "insert Row"

Glad to see your response Bob!

Much appreciated!! Please look at my code submitted after I asked the question.
For my learning process what are its problems if any?


"Bob Phillips" wrote:

See if this works for you

Private NumRows As Long

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H1:H10" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then

With Target

If Me.UsedRange.Rows.Count NumRows Then

'your code

NumRows = Me.UsedRange.Rows.Count
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
NumRows = Me.UsedRange.Rows.Count
End Sub

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default VBA to acsertain if last w/s change was "insert Row"

The following seems to work. That said, is there a better/smarter way?

IN THE GENERAL MODULE: (I had this in the worksheet module originally)

Public X As Long
Function OrigRows() As Long
X = Sheets("Trial Balance Current").UsedRange.Rows.Count
End Function


IN THE WORKSHEET MODULE:

Private Sub Worksheet_Change(ByVal Target As Range)

If Sheets("Trial Balance Current").Rows.Count X Then
Dim myRange As Range
ActiveSheet.Unprotect Password:="xxxxx"
Set myRange = Intersect(ActiveSheet.UsedRange, Selection)
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect Password:="xxxxx", DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
AllowInsertingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If

End Sub

wrote:

2003/2007

What VBA can I use to trigger additional code if the last action was "Insert Row?"

OR

How to inhibit execution of Worksheet_Change() unless the last w/s change was insert row.

NOTE: I wish that the code be in the worksheet object module (so as to use worksheet events as the
trigger)

Notice the IF clause below. It is this code line that I need clarified. How do I capture
rows.count when the w/s is activated then measure rows.count after row-insertion. If I can, then
the code below will work fine. The issue is how to extend the life of the variable "OrigRows" from
Worksheet_Activate event so that I compare to the current rows.count after worksheet_Change event.

Is there a better way? i.e., VBA-read the last action (via ReDo list?) to trigger the If Clause
below?

'Private Sub Worksheet_Change(ByVal Target As Range)

If Sheets("Trial Balance Current").Rows.Count OrigRows Then
Dim myRange As Range
ActiveSheet.Unprotect Password:="xxxxx"
Set myRange = Intersect(ActiveSheet.UsedRange, Selection)
Selection.Locked = False
Selection.FormulaHidden = False
ActiveSheet.Protect Password:="xxxxx", DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowFormattingCells:=True, AllowFormattingRows:=True, _
AllowInsertingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End If

***********

The code I could not get to work effectively (preserve the life of the variable).


Function OrigRows() As Long
OrigRows = Sheets("Trial Balance Current").UsedRange.Rows.Count
End Function

Private Sub Worksheet_Activate()

' How do I preseve OrigRows in the function above to use in the code above?

End Sub


Any thoughts appreciated, EagleOne

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
change "true" and "false" to "availble" and "out of stock" inthestands Excel Worksheet Functions 2 July 19th 07 07:05 PM
Lost "File Menu" - now it's "Edit / View / Insert.." but no "F daves Excel Discussion (Misc queries) 3 April 24th 07 04:52 AM
Change the "Insert Function" display for mail merge Beach Lover Excel Worksheet Functions 3 November 1st 06 06:41 PM
Insert "-" in text "1234567890" to have a output like this"123-456-7890" Alwyn Excel Discussion (Misc queries) 3 October 25th 05 11:36 PM
Adding "New" "Insert" "Delete" into a workbook to change from data 1 to data 2 etc Bob Reynolds[_2_] Excel Programming 0 March 4th 04 08:52 PM


All times are GMT +1. The time now is 04:55 PM.

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

About Us

"It's about Microsoft Excel"