VBA Type Mismatch in Excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FPhoenix
    New Member
    • Jul 2007
    • 18

    VBA Type Mismatch in Excel

    Ive got a Type Mismatch error i keep getting on the same line of a function. After much testing and trying to figure out what im probably not doing right, i decided i would go alittle crazy and make sure everything was the same data type something along the lines of,
    Code:
    Dim skuAddress As Range
    With Worksheets(current_ss)
    dim test as string
    test = .Cells(1, 1).Value
    If CStr(test) <> CStr("sku") Or CStr("Model") Then
    Exit Sub
    Else
    its probably something very simple like it always is, but ive tried several combinations of the If statement including
    Code:
    If test <> "sku" Or "Model" Then
    If .Cells(1, 1).Value <> "sku" Or "Model" Then
    now, ive checked it out, the value of that cell is the word sku, the variable passes the correct phrase, and "seems" identical to the check but something isnt matching up, suggestions guys?
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    when you use AND or OR in VB, you must write complete statements, i.e.

    if test = "one" or "two" then

    will give you a mistake since "two" is not a boolean or a complete statement.

    if test= "one" or test="two" then

    is the right way.

    HTH

    Comment

    • FPhoenix
      New Member
      • Jul 2007
      • 18

      #3
      bwahaha, your a genious, well maybe not, but close :-P thanks

      Comment

      • FPhoenix
        New Member
        • Jul 2007
        • 18

        #4
        another quick thought came to mind, i have to have a case statement that repeats alot of code twice, so to consolidate would it be possible to do something similar to the If statement and do
        Code:
        case "sku" or "model"
                 blah blah wicked cool and complicated code
        or would i have to split those cases up

        Comment

        • kadghar
          Recognized Expert Top Contributor
          • Apr 2007
          • 1302

          #5
          Originally posted by FPhoenix
          another quick thought came to mind, i have to have a case statement that repeats alot of code twice, so to consolidate would it be possible to do something similar to the If statement and do
          Code:
          case "sku" or "model"
                   blah blah wicked cool and complicated code
          or would i have to split those cases up
          no, im afraid you're not able to put that on a case, but, you can always use booleans, or use other alternatives:

          [CODE=vb]dim Boo1 as boolean

          '[...]
          case "sku": boo1=true
          case "model" : boo1=true
          '[...]
          end select

          if boo1 then
          '[a lot of code]
          end if[/CODE]

          HTH

          Comment

          Working...