+ Reply to Thread
Results 1 to 3 of 3

SELECT CASE with INSTR is it possible???

Hybrid View

  1. #1
    Registered User
    Join Date
    07-25-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    29

    SELECT CASE with INSTR is it possible???

    I have code that checks for a string of data with in another string. I am new to Excel VB programming so I don't know if this is supposed to work or not. The string being checked does have the value of the search argument, however the Case does not happen. It is always going to the Case Else
    Here is the code:

    
    Sub assignColforSSNandAction() 
    
        Dim strPathFileName As String 
        Dim index2 As Integer 
        Dim iFirstRow As Long 
        Dim iLastRow As Long 
        
    iFirstRow = 1 
        'Get last Row that has data 
    iLastRow = Application.WorksheetFunction.CountA(Range("A:A")) 
    
        'Loop through SSN IFS worksheet to identify the folders and files that need to have SSN cleared 
        For index2 = iFirstRow To iLastRow 
        
            Range("A" & index2).Select 
            strPathFileName = ActiveCell.Text 
            If Dir(strPathFileName) <> "" Then 
                Select Case strPathFileName 
                    Case (InStr(1, strPathFileName, "Not Matched", vbTextCompare) > 0) 
                        Range("B" & index2).Select 
                            ActiveCell.Text = "B" 
                        Range("C" & index2).Select 
                            ActiveCell.Text = "A" 
                    Case (InStr(1, strPathFileName, "\Member", vbTextCompare) > 0) 
                        Range("B" & index2).Select 
                            ActiveCell.Text = "Q" 
                        Range("C" & index2).Select 
                            ActiveCell.Text = "B" 
                    Case (InStr(1, strPathFileName, "MEDICAID", vbTextCompare) > 0) 
                        Range("B" & index2).Select 
                            ActiveCell.Text = "E" 
                        Range("C" & index2).Select 
                            ActiveCell.Text = "B" 
                    Case (InStr(1, strPathFileName, "Primary", vbTextCompare) > 0) 
                        Range("B" & index2).Select 
                            ActiveCell.Text = "C" 
                        Range("C" & index2).Select 
                            ActiveCell.Text = "B" 
                    Case (InStr(1, strPathFileName, "Supplemental", vbTextCompare) > 0) 
                        Range("B" & index2).Select 
                            ActiveCell.Text = "C" 
                        Range("C" & index2).Select 
                            ActiveCell.Text = "B" 
                    Case Else: 
                    
                        Range("D" & index2).Activate 
                            ActiveCell.Value = "Failed" 
                        Range("E" & index2).Activate 
                            ActiveCell.Value = "No Assignment of SSN/Action Column assigned: " + _ 
                            (strPathFileName) 
                ' MsgBox "No Assignment of SSN/Action Column assigned: " + (strPathFileName), vbInformation 
                End Select 
    
            Else 
                Range("D" & index2).Activate 
                    ActiveCell.Value = "Failed" 
                Range("E" & index2).Activate 
                    ActiveCell.Value = "There is no file with this name: " + (strPathFileName) 
            End If 
        Next index2 
        
        MsgBox "SSN IFS Clean-up complete" 
    End Sub
    Thanks for your replies!

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: SELECT CASE with INSTR is it possible???

    Unless I'm misreading your code, I believe that you have a mistaken understanding of the SELECT CASE function.

    SELECT CASE XXX treats XXX as the argument, and the CASE is treated as the validation. In this example, SELECT CASE strPathFile seems to be a text entry. In that syntax, each CASE argument checks if the result of your formula is strPathFile. However, (again, unless I'm misreading the code), each of the CASE formula arguments returns a TRUE/FALSE.

    Try chaning the first line of the SELECT CASE to

    SELECT CASE TRUE
    instead of
    SELECT CASE strPathFileName
    See if that works. If not, post a sample workbook with dummy data (in xls format please)

  3. #3
    Registered User
    Join Date
    07-25-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: SELECT CASE with INSTR is it possible???

    The suggestion you made work fine. I changed the value in the main select to true and it worked fine. Thank you for your response!!

+ 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