LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Desire ChangeEvent not triggered if ChangeEvent was column (field)insertion

There's no reason to use:

Set myRange = Range(Target.Address)

And don't use ActiveSheet. Use Me. That keyword represents the object owning
the code. In this case, the worksheet that just got changed.



wrote:

Hello Dave,

I had changed my code to:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'
'
Dim myCell As Range
Dim myRange As Range
Dim myTime As String
Dim IndexColumn As Long

myTime = Now()
With ActiveSheet
IndexColumn = .Cells.Find(What:="Index Key", After:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
End With
Set myRange = Range(Target.Address)
Debug.Print Target.Count
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
If Target.Count = 1 Then
For Each myCell In myRange
With Me.Cells(myCell.Row, IndexColumn + 1)
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
.Value = myTime
End With
Next myCell
End If
Cells(1, IndexColumn + 1).Value = "Date Last Update"

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

That said, I prefer your If clause. Thanks!

Dave Peterson wrote:

You can stop the rest of the code from running if the target is an entire column
(inserting or deleting!) with something like:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim myCell As Range
Dim myTime As String

If Target.Address = Target.EntireColumn.Address Then
Exit Sub
End If

myTime = Now

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

For Each myCell In Target.Cells
With Me.Cells(myCell.Row, "DZ")
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
.Value = myTime
End With
Next myCell

Me.Range("DZ1").Value = "Date Last Update"

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub

But if you insert or delete a column before column EA, then won't your tracking
column move?.

=========
If you want the column to be more "fluid", you could name a cell in that
tracking column. Select the cell or entire column (DV) and use
Insert|Name|define.

Give it a nice sheet level name.

Names in workbook: Sheet1!LastUpdateCol
Refers to: =Sheet1!$DV$1

Then the code would change to something like:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim myCell As Range
Dim myTime As String
Dim myTrackCol As Range

Set myTrackCol = Nothing
On Error Resume Next
Set myTrackCol = Me.Range("LastUpdateCol")
On Error GoTo 0

If myTrackCol Is Nothing Then
MsgBox "Design error!" & vbLf _
& "Please contact EagleOne at xxxx."
Exit Sub
End If

If Target.Address = Target.EntireColumn.Address Then
Exit Sub
End If

myTime = Now

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

For Each myCell In Target.Cells
With Me.Cells(myCell.Row, myTrackCol.Column)
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
.Value = myTime
End With
Next myCell

Me.Cells(1, myTrackCol.Column).Value = "Date Last Update"

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub





wrote:

2003 2007

What would the VBA Syntax to eliminate (from ChangeEvent) i.e. the addition of a Column within the
Target Range? The issue is that all cells in that column would be populated eith a time/date stamp.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'
'
Dim myCell As Range
Dim myRange As Range
Dim myTime As String

myTime = Now()
Set myRange = Range(Target.Address)
With Application
.EnableEvents = False
.Screenupdating = False
End with
For Each myCell In myRange
With Me.Cells(myCell.Row, "DZ")
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
.Value = myTime
End With
Next myCell
Range("DZ1").Value = "Date Last Update"
With Application
.ScreenUpdating = True
.EnableEvents = True
End with
End Sub

TIA EagleOne


--

Dave Peterson
 
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
Blocking column insertion Rafi Excel Programming 4 March 2nd 09 06:05 PM
Custom Context menu (Right click menu) not working in sheet changeevent. Madiya Excel Programming 3 February 11th 08 01:24 PM
ChangeEvent Shawn Excel Programming 1 January 18th 06 09:27 PM
Create event triggered by Pivot Chart field button Mike Collard Excel Programming 0 January 28th 04 12:00 PM
Text following an Outlook field insertion Shauna Koppang Excel Programming 0 August 25th 03 10:45 PM


All times are GMT +1. The time now is 08:14 AM.

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"