Consulting

Results 1 to 3 of 3

Thread: Loop through all rows with data not just first row

  1. #1
    VBAX Regular
    Joined
    Oct 2014
    Posts
    95
    Location

    Loop through all rows with data not just first row

    This macro to split up strings is written to deal with the first row only.

    Only column A has data, but the number of rows will vary. There are no gaps.

    Please could you modify the code so that it loop through all rows. I have tried to do this and have not succeeded.

    Thanks.

    Sub Split()
    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant
    txt = ActiveCell.Value
    FullName = Split(txt, "ADD")
    For i = 0 To UBound(FullName)
    Cells(1, i + 1).Value = FullName(i)
    Next i
    End Sub

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    I am unclear what you want. This code iterates column A and if ADD is in it, it treats it as a delimiter. e.g.
    A1=1ADD2. Then after a run, A1=1, B1=2.

    I do recommend not using reserved command words as your names.
    Sub sSplit()  
      Dim c As Range, i As Long, s() As String
      For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
        s() = Split(c.Value, "ADD")
        c.Resize(1, UBound(s) + 1).Value = s()
      Next c
    End Sub

  3. #3
    VBAX Regular
    Joined
    Oct 2014
    Posts
    95
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    I am unclear what you want. This code iterates column A and if ADD is in it, it treats it as a delimiter. e.g.
    A1=1ADD2. Then after a run, A1=1, B1=2.

    I do recommend not using reserved command words as your names.
    Sub sSplit()  
      Dim c As Range, i As Long, s() As String
      For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
        s() = Split(c.Value, "ADD")
        c.Resize(1, UBound(s) + 1).Value = s()
      Next c
    End Sub
    This is perfect, thank you Kenneth.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •