简体   繁体   中英

Looping through the rows until last row

Hello I am new to VBA and I have an excel sheet as shown in the image below and I want to run through each row until the last row to remove the characters in front of "/".

Excel 工作表示例

The following is my VBA code. However, my code could only work on one cell currently and I am not sure how do I change it to a For Loop to run through all the rows until the last row. Also, there are certain cells that does not contain the "/" and I just want it to be an empty cell. I need some help on how could I work on this? Thank you really appreciate if anyone would be able to assist me with this.

Sub String()
' String
Dim stringOriginal As String
stringOriginal = Range("A2").Value

Dim indexOfThey As Integer
indexOfThey = InStr(1, stringOriginal, "/")

Dim finalString As String
finalString = Right(stringOriginal, Len(stringOriginal) - indexOfThey)

Range("A2").Value = finalString

End Sub

Try following sub

Sub StringOperation()
Dim lRow As Long
Dim Rng As Range

lRow = Cells(Rows.Count, 1).End(xlUp).Row

For Each Rng In Range("A1:A" & lRow)
    If (InStr(1, Rng, "/")) > 0 Then
        Rng = Right(Rng, Len(Rng) - InStr(1, Rng, "/"))
    End If
Next

End Sub

Edit: To empty cells that doesn't contain / use below codes.

Sub StringOperation()
Dim lRow As Long
Dim Rng As Range

lRow = Cells(Rows.Count, 1).End(xlUp).Row
    For Each Rng In Range("A1:A" & lRow)
        If (InStr(1, Rng, "/")) > 0 Then
            Rng = Right(Rng, Len(Rng) - InStr(1, Rng, "/"))
              Else
            Rng = ""
        End If
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM