Solved

combo box dependant userform submit button

Posted on 2016-11-17
24
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 11
  • 2
24 Comments
 
LVL 47

Assisted Solution

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

Which line is giving you the error?
0
 

Author Comment

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

Author Comment

by:david francisco
ID: 41892305
the basic goal is the value in nhnNme changes, the sheet changes for the data to be copied to the correct sheet.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41892306
What is NHdata?
0
 

Author Comment

by:david francisco
ID: 41892308
the name of my userform
0
 
LVL 47

Assisted Solution

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

Author Comment

by:david francisco
ID: 41892312
the object is a combobox
0
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41892317
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 47

Assisted Solution

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

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
ID: 41892526
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
ID: 41893185
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 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41893207
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
 
LVL 19

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
ID: 41893240
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
ID: 41893432
how would I go about posting a copy of that for your viewing?
0
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41893448
Click it
0
 

Author Comment

by:david francisco
ID: 41893752
here is the file
0
 

Author Comment

by:david francisco
ID: 41893753
here is the file
new-customer-lead-tool.xlsm
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 250 total points
ID: 41893766
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 47

Assisted Solution

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

Author Comment

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

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41893786
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
ID: 41893787
AMAZING! You're the best!!!!
0
 

Author Closing Comment

by:david francisco
ID: 41893789
AMAZING! got ALL the answers I needed!
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41893792
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculating Sales Tax 13 66
autofill formulas using macro 8 52
Excel - Active X Checkboxes Groups 45 34
Assigning multiple macro in one command button 7 16
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 article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

732 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