Consulting

Results 1 to 5 of 5

Thread: VBA Select Case

  1. #1
    VBAX Regular
    Joined
    Jul 2010
    Posts
    9
    Location

    VBA Select Case

    My question is a continuation from this post: http://social.answers.microsoft.com/...0-083be3bc1cdb because I so confused the issue I’m restarting.

    I have a column of metal codes as 2 character strings to be grouped as A, B, or C. The first leg of the “If … Then” loop tests for category “A” based on metal codes: "1?", "2T", "4B", "4C", "4E", "4F", "4G", "4J", "4L", "4R", "4S", "4T", "4U", "4W", "4X", "4Y", "8P", "8T", "8U", "8W", "8X", "8Y", "8Z" I assumed that the wildcard character “*” could be used as: “1*”, “4*” & “8*” and HansV proposed a great macro wherein I found an error on my assumption. While the code “1” may take the wildcard “*” “4” and “8” may not because of many variations with the metals that places them in B or C category.

    In place of the If… Then procedure I wrote a Select Case procedure but I run into a problem: groups B and C are contingent on whether column G contains a P for the B group or M or the C group (Group A may take either). Can the 2nd variable testing for P or M in column G be nested into the Select Case code here.

    [vba]Sub KCCodeSelect()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Range("H:H").Insert
    Range("H1") = "KC Code"
    m = Range("F" & Rows.Count).End(xlUp).Row
    For r = 2 To m
    Select Case Range("F" & r)
    Case "1?", "2T", "4B", "4C", "4E", "4F", "4G", "4J", "4L", "4R", "4S", "4T", "4U", "4W", "4X", "4Y", "8P", "8T", "8U", "8W", "8X", "8Y", "8Z"
    Range("H" & r) = "A"
    Case "A*", "CO", "ET", "LD", "P*", "RR", "S*", "T*", "XX"
    Range("H" & r) = "B"
    Case "A*", "CO", "ET", "LD", "P*", "RR", "S*", "T*", "XX", "4)", "8)", "8&", "8-", "8/", "8>", "8\", "8¦", "8<", "8}", "8H", "8I", "8O", "AP", "TP", "TR"
    Range("H" & r) = "C"
    End Select
    Next r
    Application.ScreenUpdating = True
    End Sub



    Sub KCCodeSelect()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Range("H:H").Insert
    Range("H1") = "KC Code"
    m = Range("F" & Rows.Count).End(xlUp).Row
    For r = 2 To m
    Select Case Range(“F” & r)
    Case “1*”, “2T”, “4B”, “4C”, “4E”, “4F”, “4G”, “4J”, “4L”, “4R”, “4S”, “4T”, “4U”, “4W”, “4X”, “4Y”, “8P”, “8T”, “8U”, “8W”, “8X”, “8Y”, “8Z”
    Range("H" & r) = "A"
    Case “A*”, ”CO”, “ET”, “LD”, “P*”, “RR”, “S*”, “T*”, “XX”
    Range("H" & r) = "B"
    Case “A*”, “CO”, “ET”, “LD”, “P*”, “RR”, “S*”, “T*”, “XX” , “4)”, “8)”, “8&”, “8-“, “8/”, “8>”, “8\”, “8¦”, “8<”, “8}”, “8H”, “8I”, “8O”, “AP”, “TP”, “TR”
    Range("H" & r) = "C"
    End Select
    Next r
    Application.ScreenUpdating = True
    End Sub[/vba]

    Thanks EVan
    Last edited by Bob Phillips; 02-12-2011 at 02:56 PM. Reason: Added VBA Tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just nest a second select within the first.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jul 2010
    Posts
    9
    Location
    Just Exactly how to write a nested Case Select was part of my problem. Is it necessary to add a Case Else before the nested Case Select? Is this correct?


    [VBA]Sub KCCodeSelect()
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Range("H:H").Insert
    Range("H1") = "KC Code"
    m = Range("F" & Rows.Count).End(xlUp).Row
    For r = 2 To m
    Select Case Range(“F” & r)
    Case “1*”, “2T”, “4B”, “4C”, “4E”, “4F”, “4G”, “4J”, “4L”, “4R”, “4S”, “4T”, “4U”, “4W”, “4X”, “4Y”, “8P”, “8T”, “8U”, “8W”, “8X”, “8Y”, “8Z”
    Range("H" & r) = "A"
    Case “A*”, ”CO”, “ET”, “LD”, “P*”, “RR”, “S*”, “T*”, “XX”
    ‘nested select case
    Select Case Range("G" & r)
    Case “P”
    Range("H" & r) = "B"
    Case “A*”, “CO”, “ET”, “LD”, “P*”, “RR”, “S*”, “T*”, “XX” , “4)”, “8)”, “8&”, “8-“, “8/”, “8>”, “8\”, “8¦”, “8<”, “8}”, “8H”, “8I”, “8O”, “AP”, “TP”, “TR”
    ‘nested select case
    Select Case Range("G" & r)
    Case “M”
    Range("H" & r) = "C"

    End Select
    End Select
    End Select
    Next r
    Application.ScreenUpdating = True
    End Sub[/VBA]


    Thanks EVan

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It would be along the lines of

    [vba]

    Select Case condition1

    Case "A":

    Select Case condition2

    Case 1: 'do something

    Case 2; 'do another thing

    Case Else: ' do the deault
    End Select

    Case "B":

    '....
    End Select
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jul 2010
    Posts
    9
    Location
    Thanks

Posting Permissions

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