combo box dependant userform submit button

I have this code:


Code:
Private Sub sbmt_Click()
Dim irow As Long
Dim wb As Workbook
Dim ws As Worksheet
If NHdata.nhNme.Value = "JOHN" Then
Set wb = Workbooks.Open("L:\NAMES\JOHN\JOHN.xlsm")
Set ws = Worksheets("Data")
ElseIf NHdata.nhnNme.Value = "GRANT" Then
Set wb = Workbooks.Open("L:\NAMES\Grant\Grant.xlsm")
Set ws = Worksheets("Data")
End If
irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Range("A" & irow) = Date1.Value
.Range("B" & irow) = typSel.Value
.Range("C" & irow) = acntNmbr.Value
.Range("D" & irow) = nhNme.Value
.Range("E" & irow) = ojtNme.Value
.Range("F" & irow) = rslvChk.Value
.Range("G" & irow) = pvntChk.Value
.Range("H" & irow) = prmtChk.Value
.Range("I" & irow) = profChk.Value
.Range("J" & irow) = efctChk.Value
.Range("K" & irow) = srtText1.Value
.Range("L" & irow) = stpText1.Value
.Range("M" & irow) = conText1.Value
.Range("N" & irow) = srtText2.Value
.Range("O" & irow) = stpText2.Value
.Range("P" & irow) = conText2.Value
End With
wb.Save
wb.Close True
End Sub

Open in new window

i'm getting an error "13 type mismatch" is there any solution to this issue?

*update, word has it I can accomplish this with a loop, but im not very well versed in loops.
david franciscoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Martin LissOlder than dirtCommented:
I added code tags in your question.

Which line is giving you the error?
0
david franciscoAuthor Commented:
the if then statements with the set is giving me the type mismatch
0
david franciscoAuthor Commented:
the basic goal is the value in nhnNme changes, the sheet changes for the data to be copied to the correct sheet.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Martin LissOlder than dirtCommented:
What is NHdata?
0
david franciscoAuthor Commented:
the name of my userform
0
Martin LissOlder than dirtCommented:
What type of control is nhNme? Maybe it would be easier if you attached your workbook.
0
david franciscoAuthor Commented:
the object is a combobox
0
Martin LissOlder than dirtCommented:
If I understand you correctly and lines 5 and 8 are giving you the error then what you have in those lines should work as long as you have the userform and combobox names spelled correct.

Other than that I'm out of ideas unless I can see your workbook or a sample workbook that gives the same error.
0
Martin LissOlder than dirtCommented:
In line 5 you have nhNme, while in line 8 you have nhnNme. Is that correct?
0
Roy CoxGroup Finance ManagerCommented:
It looks like your code is looking for worksheet Data in the master workbook i.e. the one containing the userform, is this correct? I wouldn't think so.

When the controls are in a UserForm use the key word Me to refer to the userform. This is like a shorthand to put it simply and will kick in intellisense to avoid mistyping control names.

Use Option Explicit it will force you to declare variables.

Read up on naming conventions. It is best to use names that indicate the type of control and an idea of what it contains. I would prefix ComboVoxes with cbo, so cboNames. TextBoxes perhaps tbxprofChk, Labels lbl so lblprofChk.

Is this code better

Option Explicit

Private Sub sbmt_Click()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim irow As Long
    Dim sNm As String
    ''/// this will be more dynamic, allowing you to not use the If statement and easily add names to the list
    sNm = Me.nhNme.Value
    ''/// you might need an error handler here in case the workbook is not available.
    Set wb = Workbooks.Open("L:\NAMES\" & sNm & "\" & sNm & ".xlsm")
    Set ws = wb.Worksheets("Data")

    irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
    With ws
        .Range("A" & irow) = Me.Date1.Value
        .Range("B" & irow) = Me.typSel.Value
        .Range("C" & irow) = Me.acntNmbr.Value
        .Range("D" & irow) = Me.nhNme.Value
        .Range("E" & irow) = Me.ojtNme.Value
        .Range("F" & irow) = Me.rslvChk.Value
        .Range("G" & irow) = Me.pme.vntChk.Value
        .Range("H" & irow) = Me.prmtChk.Value
        .Range("I" & irow) = Me.profChk.Value
        .Range("J" & irow) = Me.efctChk.Value
        .Range("K" & irow) = Me.srtText1.Value
        .Range("L" & irow) = Me.stpText1.Value
        .Range("M" & irow) = Me.conText1.Value
        .Range("N" & irow) = Me.srtText2.Value
        .Range("O" & irow) = Me.stpText2.Value
        .Range("P" & irow) = Me.conText2.Value
    End With
    ''/// close and save
    wb.Close True
End Sub

Open in new window

0
david franciscoAuthor Commented:
I attempted to run the code  that Roy_Cox posted, but I keep getting a "Run-time error '1004':
Method 'range' of object '_Worksheet' failed"
0
Martin LissOlder than dirtCommented:
This would be much easier if we had a workbook to test with but put a breakpoint on line 17 (by clicking in the left-hand margin). Run the code and when it stops at the breakpoint, hover the mouse over iRow. Is it zero?
0
Roy CoxGroup Finance ManagerCommented:
Have you got the data worksheet in the master workbook or in the other workbooks?

Explain how you are using your method, not the code but what you are trying to do. As martin says an example workbook would make helping easier.
0
david franciscoAuthor Commented:
how would I go about posting a copy of that for your viewing?
0
Martin LissOlder than dirtCommented:
Click it
0
david franciscoAuthor Commented:
here is the file
0
david franciscoAuthor Commented:
here is the file
new-customer-lead-tool.xlsm
0
Martin LissOlder than dirtCommented:
Since we don't have the destination workbooks, try this change. Note that line 34 is comment out because you don't seem to have a control with that name.

Private Sub sbmt_Click()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim irow As Long
    'new
'If NHdata.nhNme.Value = "john" Or "jordy" Or "stan" Then
'Set wb = Workbooks.Open("L:\names\helper\harold\harold.xlsm")
'Set ws = Worksheets("Data")
'
'ElseIf NHdata.nhNme.Value = "chris" Or "dave" Or "christinia" Then
'Set wb = Workbooks.Open("L:\names\helper\jose\jose.xlsm")
'Set ws = Worksheets("Data")
'End If
    Select Case NHdata.nhNme.Value
        Case "john", "jordy", "stan"
             Set wb = Workbooks.Open("L:\names\helper\harold\harold.xlsm")
             Set ws = Worksheets("Data")
        Case Else
            Set wb = Workbooks.Open("L:\names\helper\jose\jose.xlsm")
            Set ws = Worksheets("Data")
    End Select

    irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
    With ws
        .Range("A" & irow) = Me.Date1.Value
        .Range("B" & irow) = Me.typSel.Value
        .Range("C" & irow) = Me.acntNmbr.Value
        .Range("D" & irow) = Me.nhNme.Value
        .Range("E" & irow) = Me.ojtNme.Value
        .Range("F" & irow) = Me.rslvChk.Value
        'new
        '.Range("G" & irow) = Me.pme.vntChk.Value
        
        .Range("H" & irow) = Me.prmtChk.Value
        .Range("I" & irow) = Me.profChk.Value
        .Range("J" & irow) = Me.efctChk.Value
        .Range("K" & irow) = Me.srtText1.Value
        .Range("L" & irow) = Me.stpText1.Value
        .Range("M" & irow) = Me.conText1.Value
        .Range("N" & irow) = Me.srtText2.Value
        .Range("O" & irow) = Me.stpText2.Value
        .Range("P" & irow) = Me.conText2.Value
    End With
    ''/// close and save
    wb.Close True
End Sub

Open in new window

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
Martin LissOlder than dirtCommented:
It looks like line 34 should be
.Range("G" & irow) = Me.pvntChk.Value
0
david franciscoAuthor Commented:
It works perfectly! one last question, what if I wanted to add an additional case for more employees and helpers?
0
Martin LissOlder than dirtCommented:
Select Case NHdata.nhNme.Value
        Case "john", "jordy", "stan" 'Covers john, jordy, and stan
             Set wb = Workbooks.Open("L:\names\helper\harold\harold.xlsm")
             Set ws = Worksheets("Data")
        Case "marty' ' Just me
             Set wb = Workbooks.Open("L:\names\helper\marty\martyd.xlsm")
             Set ws = Worksheets("Data")       
        Case Else ' Covers everyone else
            Set wb = Workbooks.Open("L:\names\helper\jose\jose.xlsm")
            Set ws = Worksheets("Data")
    End Select

Open in new window

0
david franciscoAuthor Commented:
AMAZING! You're the best!!!!
0
david franciscoAuthor Commented:
AMAZING! got ALL the answers I needed!
0
Martin LissOlder than dirtCommented:
Just to let you know, you can do your If statement this way
If NHdata.nhNme.Value = "john" Or NHdata.nhNme.Value = "jordy" Or NHdata.nhNme.Value = "stan" Then

Open in new window

but the Select Case is easier.

In any case you're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
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.