Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

In Excel, How would you create a For Loop that loops within a cell?

How would you create a For Loop that loops within a specific cell? I want to make a code that loops for each 'word' (separated by spaces) within a cell. So if a cell contains the string 'the dog has fleas', I want my code to loop on 'the', then on 'dog, then on 'has' and finally on 'fleas' before stopping.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    assuming your sentence "the dog has fleas" is in range B2

    Sub BlaBlaBla()

    Dim T As String

    Dim k As String

    Dim i As Integer

    Dim r As Integer

    T = Trim(Range("B2").Text) & " "

    For i = 1 To Len(T)

    k = k & Mid(T, i, 1)

    If Mid(T, i, 1) = " " Then

    r = r + 1

    Range("B2").Cells(r, 3) = k

    k = ""

    End If

    Next

    End Sub

    ' The result

    D2 = the

    D3 = dog

    D4 = has

    D5 = fleas

Still have questions? Get your answers by asking now.