Link to home
Start Free TrialLog in
Avatar of Tim313
Tim313Flag for United States of America

asked on

Excel VBA - Trigger Macro on Cell Leave Event

For each row in my worksheet, when I leave the cell in column H, I want to trigger a macro to run a query on an Access data table (I can code the conn and sql).

What would be the event to use and how would I set the condition to specify leaving cell H any row?
Avatar of Rgonzo1971
Rgonzo1971

pls try

Insert in the appropriate worksheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column <> 8 Then

' your code
End If

End Sub

Open in new window

Regards
Hi,

pls try

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Set LstCol = ActiveWorkbook.Names("lastColumn")
On Error GoTo 0
If LstCol Is Nothing Then
    ActiveWorkbook.Names.Add Name:="lastColumn", RefersToR1C1:="=0"
    Set LstCol = ActiveWorkbook.Names("lastColumn")
End If

If Target.Column <> 8 And LstCol.Value = "=8" Then
    ' your code
End If
LstCol.Value = "=" & Target.Column
End Sub

Open in new window

Avatar of Tim313

ASKER

Rgonzo1971,

Thanks for your response. I must be missing something, the code you provided doesn't seem to work for me.

On Line 11, I replaced 'your code with MsgBox "Left Cell H" and then moved into then out of cell H without results.

I'm very new at Excel VBA, working mostly with Visual Studio (VB) and Access VBA...

Any ideas what I'm not doing correctly?
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim313

ASKER

Rgonzo1971,

Thanks, the correction did the trick!

Tim