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?
Tim313Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
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
0
Rgonzo1971Commented:
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

0
Tim313Author Commented:
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?
0
Rgonzo1971Commented:
Hi,

Here a corrected version

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Set LstCol = ActiveWorkbook.Names("lastColumn")
On Error GoTo 0
If IsEmpty(LstCol) 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

ChangeCol.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tim313Author Commented:
Rgonzo1971,

Thanks, the correction did the trick!

Tim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.