Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2
Default Capturing excel insert/delete events.

How can we capture excel insert/delete column/row event?

One way is to use "Sheet_change" event, but how will come to know, what
change caused this event to raise?
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,510
Default Capturing excel insert/delete events.

Hello Gaurav,

The following demonstration code example should point you in the right
direction for identifying if rows or columns are inserted/deleted.

Select a cell in Row 1 out of the way of your data and to the right of any
column likely to be used in your application.

Define a name for the cell and call it ColTest. (See Define names in Help if
required)

Identify the column number of the cell and enter that number in the cell.
(Easy way is to insert =Column() then Copy - Paste Special - Values over
top of itself. DO NOT leave as a formula.)

Select a cell in column A out of the way of your data and below any row
likely to be used in your application.

Define a name for the cell and call it RowTest.

Enter the row number in the cell.

(For the above do not select the far right column or the last row on the
worksheet or you will not be able to insert any columns or rows because Excel
will not allow cells with data to be pushed off the side or bottom of the
worksheet.)

Copy the below Worksheet_Change code into the VBA editor. (I assume you know
how to do this because you have already mentioned using Change Event.)

Note my comments in the code.

You need to understand that if using UsedRange anywhere in your code then
the above cells will be included and you may have to handle that situation. I
suggest that you do not use cells too far away to the right or below your
data. If using UsedRange in other code think about Resize and reduce it by
one column and one row.


Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Range("ColTest").Column = _
Range("ColTest").Value Then

'MsgBox only for demo. Normally no code
'required if no Insert/Delete
'MsgBox "No column inserted or deleted"

GoTo TestRows
End If

If Range("ColTest").Column _
Range("ColTest").Value Then

'Insert you code here in lieu
'of the MsgBox
MsgBox Range("ColTest") _
.Column - Range("ColTest") _
.Value & " Column/s inserted"

GoTo TestRows
End If

If Range("ColTest").Column < _
Range("ColTest").Value Then

'Insert you code here in lieu
'of the MsgBox
MsgBox Range("ColTest") _
.Value - Range("ColTest") _
.Column & " Column/s deleted"

GoTo TestRows
End If

TestRows:

If Range("RowTest").Row = _
Range("RowTest").Value Then

'MsgBox only for demo. Normally no code
'required if no Insert/Delete
'MsgBox "No Row inserted or deleted"

GoTo ReEnableEvents
End If

If Range("RowTest").Row _
Range("RowTest").Value Then

'Insert you code here in lieu
'of the MsgBox
MsgBox Range("RowTest") _
.Row - Range("RowTest") _
.Value & " Row/s inserted"

GoTo ReEnableEvents
End If

If Range("RowTest").Row < _
Range("RowTest").Value Then

'Insert you code here in lieu
'of the MsgBox
MsgBox Range("RowTest") _
.Value - Range("RowTest") _
.Row & " Row/s deleted"

GoTo ReEnableEvents
End If

ReEnableEvents:
Range("ColTest") = Range("ColTest").Column
Range("RowTest") = Range("RowTest").Row
Application.EnableEvents = True

End Sub

Because you are disabling events, if your code should fail during testing
then events remain turned off until you restart Excel so insert the
forllowing code into the VBA editor and run it if the code fails for any
reason. For convenience you can place it below the above code in the
worksheet module and you can run it from within the VBA editor. (You won't
see anything when it runs but it does turn the events back on)

Sub ReEnableEvents()
Application.EnableEvents = True
End Sub


--
Regards,

OssieMac


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
Capturing Insert/Delete Events in Excel Sheet using c# Gaurav Nanda[_2_] Excel Worksheet Functions 0 July 10th 09 06:14 AM
Excel 2007 - Cannot Delete or Insert Rows SCNetworks Excel Discussion (Misc queries) 1 March 24th 09 05:48 PM
How to program excel to insert / delete any worksheet? Eric Excel Worksheet Functions 1 March 2nd 08 09:49 PM
Capturing excel screenshots vsoler Excel Discussion (Misc queries) 8 June 17th 07 06:06 AM
How to Track Column Insert/Delete Event in Excel mac_excel New Users to Excel 3 July 8th 05 12:11 AM


All times are GMT +1. The time now is 11:43 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"