Link to home
Start Free TrialLog in
Avatar of Stephen Byrom
Stephen ByromFlag for Ireland

asked on

MS Excel 2010/2013

Hi,
I have an active X combo box on a worksheet that moves the cursor to the correct cell when the user selects an item from the list.
The problem I have is that whenever a cell is changed in any other worksheet, the code behind the combo box runs and the user is taken to the sheet with the combo box on it, even though nothing has changed in the combo box! I have tried adding "thisworkbook.sheets" etc but to no avail.

this is the simple code behind the active X combo box;

Private Sub cboCust_Change()
Dim c As Range
    For Each c In Range("A4:A500")
        If c.Value = cboCust.Value Then
            Application.Goto c, Scroll:=True
            c.select
            Exit Sub
        End If
    Next
End Sub

Any help is appreciated.
Thanks
Avatar of gplana
gplana
Flag of Spain image

I think you can avoid this problem just by adding an if to execute the code only if the currentcell is the cell you want.

something like:

Private Sub cboCust_Change()
Dim c As Range
   if activeCell.row=3 and activeCell.column=1 then
    For Each c In Range("A4:A500")
        If c.Value = cboCust.Value Then
            Application.Goto c, Scroll:=True
            c.select
            Exit Sub
        End If
    Next
   end if
End Sub

Open in new window

Avatar of Stephen Byrom

ASKER

Thanks GP but I tried that earlier, although with active sheet...

Dim c As Range
   If ActiveSheet = Sheets("NonAuto") Then
    For Each c In Range("A4:A500")
        If c.Value = cboCust.Value Then
            Application.Goto c, Scroll:=True
            c.Select
            Exit Sub
        End If
    Next
   End If

but that didn't work either.

I have added a snapshot of the workbook to make it easier to see what the problem is
Call-Offs.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

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
Got it.
Seems a bit clumsy but this works..
Private Sub cboCust_Change()
If ActiveSheet.Name = "NonAuto" Then
Dim c As Range
    For Each c In Range("A4:A500")
        If c.Value = cboCust.Value Then
            Application.Goto c, Scroll:=True
            c.Select
            Exit Sub
        End If
    Next
End If
End Sub
I've requested that this question be closed as follows:

Accepted answer: 0 points for olorin57's comment #a39773497

for the following reason:

trial and error for about two hours
wow,
I think we posted at the same time.
In fact if I look at the posting times I was a minute later than MacroShadow, so if a moderator wants to award the points to him/her, then I don't mind at all.
Thanks for your time