+ Reply to Thread
Results 1 to 13 of 13

Type mismatch for Userform

Hybrid View

Aland2929 Type mismatch for Userform 10-17-2011, 12:50 PM
StephenR Re: Type mismatch for Userform 10-17-2011, 01:05 PM
GeneralDisarray Re: Type mismatch for Userform 10-17-2011, 01:09 PM
Aland2929 Re: Type mismatch for Userform 10-17-2011, 01:12 PM
GeneralDisarray Re: Type mismatch for Userform 10-17-2011, 01:12 PM
Aland2929 Re: Type mismatch for Userform 10-17-2011, 01:20 PM
GeneralDisarray Re: Type mismatch for Userform 10-17-2011, 01:55 PM
Aland2929 Re: Type mismatch for Userform 10-17-2011, 02:03 PM
royUK Re: Type mismatch for Userform 10-17-2011, 02:04 PM
Aland2929 Re: Type mismatch for Userform 10-17-2011, 02:25 PM
GeneralDisarray Re: Type mismatch for Userform 10-17-2011, 02:45 PM
GeneralDisarray Re: Type mismatch for Userform 10-17-2011, 05:05 PM
Aland2929 Re: Type mismatch for Userform 10-18-2011, 01:22 AM
  1. #1
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Type mismatch for Userform

    I select a month from a drop-down list and only want to select "August" or "September". When I run this code, I get a "Compile error" - Type mismatch? The problem seems to be "August"?
    Thanks in advance



    Private Sub OKButton_Click()
    Dim MonthDB As Range
        If Range("MonthDB") Is "August" Then
            Sheets("PrincipalsAugust2011").Activate
            Range("A2").Select
            Exit Sub
        ElseIf Range("MonthDB") Is "September" Then
            Sheets("PrincipalsSeptember2011").Activate
            Range("A2").Select
            Exit Sub
        Else
            Unload Me
        End If
    End Sub
    Last edited by Aland2929; 10-19-2011 at 06:10 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Type mismatch for Userform

    Does it work with this?
    If Range("MonthDB") = "August"

  3. #3
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Type mismatch for Userform

    if MonthDB is typed as range (first of all you must SET what this variable is)

    Just use:
     IF monthDB.value ="August" then
    for your condition

    If you are passing MonthDB into range("monthDB") then monthDB should be a string representing an address...like "$A$1"...by declaring it a Range you don't have to declare the range on-the-fly
    Last edited by GeneralDisarray; 10-17-2011 at 01:13 PM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  4. #4
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Re: Type mismatch for Userform

    Thank you Stephen. That works perfectly.

  5. #5
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Type mismatch for Userform

    Remember, the range variables need to be "set"

     set monthDB = Worksheet("appropriate_sheet_name_here").RANGE("Appropriate address here")

  6. #6
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Re: Type mismatch for Userform

    Can you explain why I need to use SET in this case? Would I use:
    set monthDB = Worksheet("PrincipalsAugust2011").RANGE("August")
    set monthDB = Worksheet("PrincipalsSeptember2011").RANGE("September")
    Thank You.

  7. #7
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Type mismatch for Userform

    well, is "august" a named range, or the value of a specific cell?

    I could be wrong on this, but everytime I've ever called a range method on a variable, without first setting the variable i receive a "object variable not set" error when i try to run the sub.

    Are you still having trouble with your code or were you just asking a general question?

  8. #8
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Re: Type mismatch for Userform

    The code works fine without the SET code. The months are selected from a drop-down list and the selection is stored in the range "MonthDB".
    Thanks.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Type mismatch for Userform

    You don't need to select or activate the sheet or range. I suggest that you explain exactly what you need upload an example workbook.

    What kind of "drop down list" are you using?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  10. #10
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Re: Type mismatch for Userform

    Thanks Roy. I have attached a sample file.
    Attached Files Attached Files

  11. #11
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Type mismatch for Userform

    Ok, for named ranges...little easier (remember to use brackets around a named range's name in the code)

    EDIT ::
    (SEE ATTACHMENT ) Tested it out with a quick step through. Key is you can just access the named range, didn't need to even define a variable to pull it off. Also, did you only want 2 possible months or a list of 12? Either way you set that up with "data validations"

    (If i understand you, you want to pull up the sheet if it's there and that's it)
    Option Explicit
    
    Sub ShowMonths()
    
    On Error GoTo x:
    ThisWorkbook.Worksheets([MonthDB].Value).Activate
    Exit Sub
    
    x:
    MsgBox ("Sheet " & [MonthDB].Value & " not found.")
    
    End Sub
    Attached Files Attached Files
    Last edited by GeneralDisarray; 10-17-2011 at 03:00 PM.

  12. #12
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Type mismatch for Userform

    good...no good? dag yo, nBvc is right

    thank you's are nice to get sometimes....
    Last edited by GeneralDisarray; 10-17-2011 at 05:14 PM.

  13. #13
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    858

    Re: Type mismatch for Userform

    Thank you, I do appreciate your response. The delay was due to time difference.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1