+ Reply to Thread
Results 1 to 4 of 4

Split Function in vba

  1. #1
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Split Function in vba

    Hello there,

    I am using the Split function in my macro,but having one problem with that.

    The split separater that I am using is "," I am using this function in a msgbox where I am entering numbers: 1,2,3,4 etc.. The problem is that if I put a comma after the last number I am getting an error. Can I add some lines that will prevent that.

    Many thanks!

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Split Function in vba

    My Mac doesn't support Split, so I'm not sure on this point, is the error from the Split function or is it from your later trying to process vbNullString as a number?

    In either case, you could test for and strip off the trailing "," before Splitting.

    Please Login or Register  to view this content.
    Also, after the Split, you should use either IsNumeric or Val to protect against entries like "a,1,2,frank".
    Last edited by mikerickson; 01-31-2010 at 03:55 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Split Function in vba

    Hi, this "If" statement works fine. I need to test for two more things before splitting. Can you please help me with that?

    First, I need to make sure that no other delimiters besides the ","(comma) are used. If other delimiters are found I need the code to exit the sub Example: 1.2.3. or 1/2/3/ etc

    Second, if more than one consecutive delimiter is found any where in the msgbox, I need the code to exit the sub also. Example: 1,,2,3,4 or 1,2,3,4,,

    I need all that, because the sheet is password protected and I have lines before and after this split. If something happens between those two lines, the sheet gets unprotected.

    Thanks!

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Split Function in vba

    How do you determine the delimiter? Is anything that's not a number a delimiter?
    I suggest you simply do a split as Mike said, then validate each item. If any are invalid (non-numeric, blank etc) then pop up a message, reprotect and exit.
    Also, if you need to ensure that the sheet is reprotected no matter what, you need to have an error handler.
    Everyone who confuses correlation and causation ends up dead.

+ 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