• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 85
  • Last Modified:

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.
0
david francisco
Asked:
david francisco
  • 11
  • 11
  • 2
12 Solutions
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 11
  • 11
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now