Solved

combo box dependant userform submit button

Posted on 2016-11-17
24
27 Views
Last Modified: 2016-11-18
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
Comment
Question by:david francisco
  • 11
  • 11
  • 2
24 Comments
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
I added code tags in your question.

Which line is giving you the error?
0
 

Author Comment

by:david francisco
Comment Utility
the if then statements with the set is giving me the type mismatch
0
 

Author Comment

by:david francisco
Comment Utility
the basic goal is the value in nhnNme changes, the sheet changes for the data to be copied to the correct sheet.
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
What is NHdata?
0
 

Author Comment

by:david francisco
Comment Utility
the name of my userform
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
What type of control is nhNme? Maybe it would be easier if you attached your workbook.
0
 

Author Comment

by:david francisco
Comment Utility
the object is a combobox
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
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
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
In line 5 you have nhNme, while in line 8 you have nhnNme. Is that correct?
0
 
LVL 17

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
Comment Utility
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
 

Author Comment

by:david francisco
Comment Utility
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
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
Comment Utility
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
 

Author Comment

by:david francisco
Comment Utility
how would I go about posting a copy of that for your viewing?
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
Click it
0
 

Author Comment

by:david francisco
Comment Utility
here is the file
0
 

Author Comment

by:david francisco
Comment Utility
here is the file
new-customer-lead-tool.xlsm
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 250 total points
Comment Utility
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
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
It looks like line 34 should be
.Range("G" & irow) = Me.pvntChk.Value
0
 

Author Comment

by:david francisco
Comment Utility
It works perfectly! one last question, what if I wanted to add an additional case for more employees and helpers?
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
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
 

Author Comment

by:david francisco
Comment Utility
AMAZING! You're the best!!!!
0
 

Author Closing Comment

by:david francisco
Comment Utility
AMAZING! got ALL the answers I needed!
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now