+ Reply to Thread
Results 1 to 32 of 32

Select Case Statement

  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:

    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.
    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.
    Please Login or Register  to view this content.
    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.
    Please Login or Register  to view this content.
    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

  8. #8
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Leith, I have that.

    Hi protonLeah, I also have that.

    (Sorry, the inadvertently omitted them in my post - I had remove them to try to figure out the problem.)

    However, I am still getting the error that MS Excel cannot access the file. The file is saved as a ".csv". I know that the path is correct and it is not being used.

    Any idea for the error?

    Thank you,
    Gos-C

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    I seem to recall that spaces are not allowed in http addresses, they use either the underscore or the %20 to separate words. Are you sure your file names are correct? For instance, can you try:

    Please Login or Register  to view this content.
    and elsewhere there are spaces in the filename.

  10. #10
    Registered User
    Join Date
    03-30-2005
    Posts
    7

    Avoid using spaces or underscores

    Avoid using spaces or underscores in such codings.

    I had similar trouble in which I used Underscore. But it is not HTTP link. After removing the underscore from the file name, it worked fine.
    Kumar

  11. #11
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    OK, I found an extra space in the folder name, which was causing the problem: I had OSAM Z Files (two spaces between OSAM and Z) for the folder name and OSAM Z Files (one space between OSAM and Z) in my code. Thank you for the tip, protonLeah.

    So, now the file is opening but I am getting Run-time error '13': Type mismatch at the following line:

    Please Login or Register  to view this content.
    Here is the code I ran:

    Please Login or Register  to view this content.
    Can someone please tell me how to fix it.

    Thanks,
    Gos-C

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Try this.
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Hi Gos-C

    I noticed this in your code:

    Please Login or Register  to view this content.
    You could change that to:

    Please Login or Register  to view this content.

    Z4747 and Z4848 both being "Saskatchewan Providers"
    Last edited by SOS; 06-07-2008 at 03:20 PM.
    Hope this helps

    Seamus

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

    Yes, that works. Thank you.

    Hi SOS,

    Thanks for pointing out the error. I corrected it to:

    Please Login or Register  to view this content.
    My code has another bug:

    The AlignData gives Run-time '9': Subscript out of range at:

    Please Login or Register  to view this content.
    Here is the code (which is in a separate module):

    Please Login or Register  to view this content.
    Can you please tell me how to fix it?

    Thank you,
    Gos-C
    Last edited by Gos-C; 06-08-2008 at 07:50 AM.

  15. #15
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    zName is not assigned a value in this sub
    Is it a public value or in the declarations area? or do you mean the sheet name is "zName"
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

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

    Thanks for your quick response. I declared zName as public in the here:

    Please Login or Register  to view this content.
    zName would be both the workbook and the worksheet name.

    Gos-C

  17. #17
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    The subscript out of range error probably means that the worksheet zName does not exist, or there is something wrong with the calculated values for n or Lastrow.

    To troubleshoot, please put a breakpoint in your code at the line where it crashes, then add watches for c, zName, n and Lastrow to see if the values are what you expected/intended (including spelling and spaces). You might also Dim n as Long (your loop control var). You might try Dim'ing c as Variant vs. Range.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Where is all this code located?

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

    It's working! Thanks for you suggestions.

    My codes were located as follows:

    commandbutton1_click in - Sheet1,
    DeleteColumns - Module1
    SortColumns and Proper_Case - Module2
    ColumnSort2, Headings2 and FormatSheet - Module3
    AlignData - Module4

    After reading you posts, I moved AlignData to Sheet1, and declared n as long . . . that did it. It worked!

    One more question, please: How can I exit if I click the Cancel button for the input box here?

    Please Login or Register  to view this content.
    Thanks,
    Gos-C

  20. #20
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    If zName="" Then....

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

    The Input Box has an OK button and a Cancel button. Regardless of whether zName = "", when a user clicks the Cancel button, I want "Exit Sub" to occur.

    Gos-C
    Last edited by Gos-C; 06-09-2008 at 07:26 PM.

  22. #22
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    When this form of the inputbox receives a "cancel" it returns "" to the receiving variable regardless of whether anything had been typed into the box. So the line:
    Please Login or Register  to view this content.
    should work for you, it does on my machine.

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

    You are right, it works!

    Thank you,
    Gos-C

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

    My code starts giving me an error:

    A Microsoft Visual Basic window come up containing an X and 400. Does any know what that means?

    Thanks,
    Gos-C

  25. #25
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    From:
    http://support.microsoft.com/kb/146864

    I found:
    400 Form already displayed; can't show modally (version 97)

  26. #26
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Thanks for the info, protonLeah.

    Gos-C

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

    Code to Delete Column

    Hi all,

    I am getting "Run-time error 1004: Application defined or object.defined error" with the following code:

    Please Login or Register  to view this content.
    Can you tell me why.

    Thanks,
    Gos-C

  28. #28
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    I think you will get that error if the code is in a sheet module and a different sheet is active. So, include the worksheet name, e.g., worksheets("sheet1").Range("H:H").Delete

  29. #29
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    protonLeah, you are the best! That was it; it works now.

    Thanks for guiding me along. I am learning a lot.

    Gos-C

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

    Find/Create Folder and Save As

    Hi all,

    I am down to the last part of my code, and I need your help again.

    Here is my code:

    Please Login or Register  to view this content.
    I am getting Run-time error '52': Bad file name or number

    How can I accomplish what I want?

    Thank you,
    Gos-C
    Last edited by Gos-C; 06-16-2008 at 12:00 PM.

  31. #31
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953
    As a test, I replaced the http:// with C:/ and the error went away. HTTP... is a URL so the DIR function won't work, but I don't have any experience with VB and URL connections to help you with, .

  32. #32
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Thanks, protonLeah.

    Can anyone else help me with this?

    Thank you,
    Gos-C
    Last edited by Gos-C; 06-18-2008 at 09:43 AM.

+ 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