Looping a String Split through Column

2020-05-03 11:36发布

I have a column full of data in a format I don't like, and need them in another format. Currently they are formatted like this: "190826_095630_3E_1 (ROI 0)" and I need just the "3E" portion. I have written a string split that uses the "_" and I figure I can then just take the column of data that is produced that I want, however I can only get this to work one cell at a time while I click each one. I tried to write a for loop but I am running into trouble, most likely because I used "active.cell". Does anyone have a better way to loop this split through my column? Alternatively if you also know how to just return the third string split (3E) I would really appreciate it.

'No loop: This works for one cell

Option Explicit

Sub NameTest()
    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant


    txt = ActiveCell.Value

    FullName = Split(txt, "_")

    For i = 0 To UBound(FullName)

        Cells(1, i + 1).Value = FullName(i)

    Next i


End Sub


'Attempt at a loop: 
Option Explicit

Sub NameTest()
    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim x As Integer

    For x = 1 To 1000

    txt = ActiveCell.Value

    FullName = Split(txt, "_")

    For i = 0 To UBound(FullName)

        Cells(1, i + 1).Value = FullName(i)

    Next i
    Next x

End Sub

I would like to get this to run until the last cell with data in a given column.

0条回答
登录 后发表回答