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

asked on

Selecting a cell

Hi,
I am tring to get a form to load and fill some txtboxes with certain text, depending on the selected cell which are all named (cMakita, cELC) etc.
The code I have got so far is attached but I cannot get it to work. I think it maybe the active cell or target line.
As always any help is much appreciated.
Thanks
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrHandler
Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Set a = Range("cMakita")
Set b = Range("cIndesit")
Set c = Range("cMilsco")
Set d = Range("cELC")
    Select Case Target.Address
        Case a
            Load MainOrder
            MainOrder.txtCust = "MAKITA"
            MainOrder.cboProd.RowSource = ("tblMakita")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case b
            Load MainOrder
            MainOrder.txtCust = "INDESIT"
            MainOrder.cboProd.RowSource = ("tblIndesit")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case c
            Load MainOrder
            MainOrder.txtCust = "MILSCO"
            MainOrder.cboProd.RowSource = ("tblMilsco")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case d
            Load MainOrder
            MainOrder.txtCust = "ELC"
            MainOrder.cboProd.RowSource = ("tblElc")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Else
            'do nothing
    End Select
Exit_ErrHandler:
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume Exit_ErrHandler
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
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
BTW the following probably won't work because a form has to be visible before you can set focus to a field, so reverse them.

MainOrder.cboProd.SetFocus
MainOrder.Show
Thanks for the compliment and helpful vote for my article. Let me know if there's anything more I can do here.
Avatar of Stephen Byrom

ASKER

Thanks Martin.
The article is a good reference for a beginner like me. Is there a way I can add it to my knowledge base here?

On the problem in question;
I did as you said and the address is "$A$4" when I select the cell A4, which has a name of "cMakita".
The reason I wanted to try and get it to load using the named ranges of the cells rather than A4 or A16 etc was that there will be a time in the near future where lines will be inserted in the worksheet and the address will change.
Using named ranges should be fine.

When the code gets to the breakpoint at line 11 press F8 repeatedly to step through the code execution line by line. Does it do what you expect it to do?
No, The form loads no matter which cell I select.
And the frorm is not populated as I wanted nor is the rowsource updated.
Now it doesn't show at all no matter which cell I select
Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Set a = Range("cMakita")
Set b = Range("cIndesit")
Set c = Range("cMilsco")
Set d = Range("cELC")
    Select Case Target.Address
        Case a
            Load MainOrder
            MainOrder.txtCust = "MAKITA"
            MainOrder.cboProd.RowSource = ("tblMakita")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case b
            Load MainOrder
            MainOrder.txtCust = "INDESIT"
            MainOrder.cboProd.RowSource = ("tblIndesit")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case c
            Load MainOrder
            MainOrder.txtCust = "MILSCO"
            MainOrder.cboProd.RowSource = ("tblMilsco")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case d
            Load MainOrder
            MainOrder.txtCust = "ELC"
            MainOrder.cboProd.RowSource = ("tblElc")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Else
            'do nothing
    End Select

Open in new window

Can you attach your workbook and list the steps necessary to recreate your problem?
It's a very large workbook and I have a pitiful internet connection Martin. (reminds me of dial-up)

I managed to get the form to load using the static addresses.
    Select Case Target.Address
        Case ("$A$4")
            Load MainOrder
            MainOrder.txtCust = "MAKITA"
            MainOrder.cboProd.RowSource = ("tblMakita")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case ("$A$12")
            Load MainOrder
            MainOrder.txtCust = "INDESIT"
            MainOrder.cboProd.RowSource = ("tblIndesit")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case ("$A$18")
            Load MainOrder
            MainOrder.txtCust = "MILSCO"
            MainOrder.cboProd.RowSource = ("tblMilsco")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case ("$A$16")
            Load MainOrder
            MainOrder.txtCust = "ELC"
            MainOrder.cboProd.RowSource = ("tblElc")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Else
            'do nothing
    End Select

Open in new window

Thanks for your time
The article alone is worth it
Here are two ways that will work with the ranges. Note that in neither case do you need to two commented-out lines in the error handling.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrHandler
Dim a As Range
Dim b As Range
Dim c As Range
Dim d As Range
Set a = Range("cMakita")
Set b = Range("cIndesit")
Set c = Range("cMilsco")
Set d = Range("cELC")
    Select Case Target.Address
        Case a.Address
            Load MainOrder
            MainOrder.txtCust = "MAKITA"
            MainOrder.cboProd.RowSource = ("tblMakita")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case b.Address
            Load MainOrder
            MainOrder.txtCust = "INDESIT"
            MainOrder.cboProd.RowSource = ("tblIndesit")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case c.Address
            Load MainOrder
            MainOrder.txtCust = "MILSCO"
            MainOrder.cboProd.RowSource = ("tblMilsco")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case d.Address
            Load MainOrder
            MainOrder.txtCust = "ELC"
            MainOrder.cboProd.RowSource = ("tblElc")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Else
            'do nothing
    End Select
'Exit_ErrHandler:
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    'Resume Exit_ErrHandler
End Sub

Open in new window


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrHandler
    Select Case Target.Address
        Case Range("cMakita").Address
            Load MainOrder
            MainOrder.txtCust = "MAKITA"
            MainOrder.cboProd.RowSource = ("tblMakita")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Range("cIndesit").Address
            Load MainOrder
            MainOrder.txtCust = "INDESIT"
            MainOrder.cboProd.RowSource = ("tblIndesit")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Range("cMilsco").Address
            Load MainOrder
            MainOrder.txtCust = "MILSCO"
            MainOrder.cboProd.RowSource = ("tblMilsco")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Range("cELC").Address
            Load MainOrder
            MainOrder.txtCust = "ELC"
            MainOrder.cboProd.RowSource = ("tblElc")
    MainOrder.txtCust.Locked = True
    MainOrder.cboProd.SetFocus
    MainOrder.Show

        Case Else
            'do nothing
    End Select
'Exit_ErrHandler:
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    'Resume Exit_ErrHandler
End Sub

Open in new window

Thanks again for your time.
It was this line that I didn't have right....
        Case Range("named range").Address
Thanks for the lesson
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013