Results 1 to 5 of 5

Unable to decipher what is wrong with the loop

Threaded View

  1. #1
    Registered User
    Join Date
    01-15-2020
    Location
    Philippines
    MS-Off Ver
    365
    Posts
    3

    Post Unable to decipher what is wrong with the loop

    I'm writing a function that will get a string and split it into words and apply formatting to each word accordingly (based on my rules).
    For example: JOHN ALPHA BRAVO will be John Alpha Bravo and AD67 TRIALS willl become AD67 Trials. Something like that.

    Anyway, I'm only in the beginning of my code and can't quite comprehend why it won't work.
    I wrote the following code to split the text and put them into an array. I can fill the 1st slot in the array but the For loop won't continue.
    It does not return any error whatsoever so I really don't know what to do.

    Public Function test(source As String) As Variant 'test(John Alpha Bravo)
        
        Dim words() As String
        Dim word_count As Integer
        Dim spaces() As Integer
            
        word_count = Len(source) - Len(Replace(source, " ", "")) 'word_count = 2
        ReDim words(word_count) 'words(2)
        ReDim spaces(word_count) 'words(2)
        
        For counter = 0 To word_count 'counter = 0 To 2
            spaces(counter) = (InStr(1, source, " ")) 'spaces(0) = 5
            words(counter) = Mid(source, 1, spaces(counter) - 1) 'words(0) = "John"
            source = Mid(source, ((spaces(counter)))) 'source = " Alpha Bravo"
        Next counter
        
        test = source
                   
    End Function
    At the moment, words(0) is equal to John, words(1) is blank and so is words(2).

    Thank you in advance for anyone that'll take their time and help me!
    Attached Files Attached Files
    Last edited by 6StringJazzer; 01-15-2020 at 01:24 PM. Reason: Changed QUOTE tags to CODE tags

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Help - Unable to figure out what is wrong with the code
    By goudavnash.b in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-24-2017, 03:29 AM
  2. [SOLVED] What's wrong with this loop?
    By DMA in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-15-2017, 05:18 AM
  3. Unable to LOOP
    By slxia1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-18-2014, 08:42 AM
  4. Loop through multiple files and call macros (but unable to loop)
    By ryanpetersen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-23-2014, 12:04 PM
  5. What is wrong with this for loop?
    By kmakjop in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-17-2014, 02:31 PM
  6. What's wrong with my loop ?
    By Grek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-13-2006, 02:23 PM
  7. Loop Function unable to loop
    By Junior728 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2005, 06:05 AM

Tags for this Thread

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