+ Reply to Thread
Results 1 to 32 of 32

Select Case Statement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Select Case Statement

    Hi all,

    I have tried my best but can't get the following code to do what I want it to do:

    Option Explicit
    Dim zName As String, prov As string
    
    Private Sub commandbutton1_click()
    Dim response As Integer
    zName = InputBox("Please enter the Z-file name", "Enter Z-File Name", "Z-file name")
    response = MsgBox("Is " & UCase(zName) & " the correct file name?", vbYesNo + vbQuestion, "Z-File Name")
    If response = vbYes Then
    Call SelectProvinceCase
    Application.Workbooks.Open ("http://sharepoint/sites/hbm/Audit/OSAM Z FILES/" & prov & "/" & zName & ".csv")
    If Left(zName, 5) = "Z4343" Or "Z4545" Then
    Call AlignData
    End If
    Call DeleteColumns
    Call SortColumns
    Call ColumnSort2
    Call Headings2
    Call FormatSheet
    End Sub
    
    Sub SelectProvinceCase()
    prov = Left(zName, 5)
    Select Case prov
        Case Z4141
            prov = "Nova Scotia Providers"
        Case Z4242
            prov = "Quebec Providers"
        Case Z4343
            prov = "NFLD Providers"
        Case Z4444
            prov = "NEW Brunswick Providers"
        Case Z4545
            prov = "PEI Providers"
        Case Z4646
            prov = "Ontario Providers"
        Case Z4747
            prov = "Saskatchewan Providers"
        Case Z4848
            prov = "Saskatchewan Providers"
        Case Z4949
            prov = "Alberta Providers"
        Case Z5050
            prov = "British Columbia Providers"
        Case Else
            MsgBox "Please verify the z-file name - the provider number appears to be incorrect."
        Exit Sub
    End Select
    End Sub
    The SelectProvinceCase is not working so I know it is not written correctly.

    If the first five characters of the file name is Z4949, for example, I want Alberta Providers to be inserted where prov is in

    Application.Workbooks.Open ("http://sharepoint/sites/hbm/Audit/OSAM Z FILES/" & prov & "/" & zName & ".csv")
    so that the code will open the file.

    Can someone fix the code for me, please.

    Thank you,
    Gos-C
    Last edited by Gos-C; 06-05-2008 at 02:11 PM.
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Why aren't you using quotes for Z4141 etc in the case statements?

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Norie,

    I had quotes around them but that didn't work.

    Gos-C

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Does that code even compile?

    For a start there is no such data type as variable which you seem to have here.
    prov As Variable
    Then since you don't have quotes around Z4141 etc then VBA would probably be regarding them as variables.

    And since you have Option Explicit set then VBA should really be flagging that up.

    When you say it doesn't work, how exactly?

  5. #5
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Norie,

    Sorry, I edited my first post to show prov as String. I was changing different things just to see if I can get it to work.

    When I run the code I get Run-time error '1004'. Microsoft Office Excel cannot access the file . . . and I noticed that the file path does not include the "prov" folder, which it should have received from the Select Case Statement.

    Edited . . . I just realized I need to call SelectProvinceCase - the path is now correct, but it still is not working!

    Gos-C
    Last edited by Gos-C; 06-05-2008 at 03:35 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Gos-C,

    Here is how the syntax should be for comparing strings in a Select Case statement.
    Sub SelectProvinceCase()
    
      prov = Left(zName, 5)
        Select Case prov
          Case Is = "Z4141"
            prov = "Nova Scotia Providers"
          Case Is = "Z4242"
            prov = "Quebec Providers"
          Case Is = "Z4343"
            prov = "NFLD Providers"
          Case Is = "Z4444"
            prov = "NEW Brunswick Providers"
          Case Is = "Z4545"
            prov = "PEI Providers"
          Case Is = "Z4646"
            prov = "Ontario Providers"
          Case Is = "Z4747"
            prov = "Saskatchewan Providers"
          Case Is = "Z4848"
            prov = "Saskatchewan Providers"
          Case Is = "Z4949"
            prov = "Alberta Providers"
          Case Is = "Z5050"
            prov = "British Columbia Providers"
          Case Else
            MsgBox "Please verify the z-file name - the provider number appears to be incorrect."
            Exit Sub
       End Select
       
    End Sub
    Sincerely,
    Leith Ross

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    You seem to be missing an End If in the commandbutton1_click() sub.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Spellnumber
    By Williams in forum Excel - New Users/Basics
    Replies: 13
    Last Post: 02-16-2020, 03:34 AM
  2. Select Case Statement
    By Gos-C in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-13-2008, 09:34 AM
  3. "Translating" numbers into words
    By Portuga in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-14-2008, 11:44 AM
  4. Spell number
    By nowfal in forum Excel General
    Replies: 4
    Last Post: 08-20-2007, 04:21 PM
  5. [SOLVED] Variable Command Statment w/ Select Case
    By mpeplow in forum Excel General
    Replies: 1
    Last Post: 04-06-2007, 05:49 PM

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