Solved

combo box dependant userform submit button

Posted on 2016-11-17
24
43 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 46

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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 46

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 46

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 46

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 46

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 18

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 46

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 18

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 46

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 46

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 46

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 46

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 46

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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