简体   繁体   中英

Remove all character after nth character

I am working on some data organization and need to move information into a form. I have a column of IP addresses and some columns have more than one, or even none. I preferably need to be able to go through the column and keep the first 13 characters and remove everything after it, have 581 lines I need it to be able to run through. I think if I could get a VBA that will run through the column and find the first "," and remove that and anything after it will work. I tried this, but it did nothing.

Sub cleanup()
    lastrow = Range("G581").End(xlUp).Row

    For i = 2 To lastrow
        If Len(Cells(i, 1)) > 13 Then
            Cells(i, 1) = Left(Cells(i, 1), 14)
        End If
    Next i

End Sub

As mentioned before, you are counting the rows in column G, hopefully column G and Column A have the same number of rows. I'm thinking column G may be the issue.

lastrow = Range("A581").End(xlUp).Row

For i = 2 To lastrow
    If Len(Cells(i, 1)) > 13 Then
        Cells(i, 1) = Left(Cells(i, 1), 14)
    End If
Next i

Honestly there could be a few reasons that you're having trouble. The big one is that when you use the Left() function, you tell it to keep 14 characters instead of the 13 you said that you want!

Another option could be that excel thinks you are trying to do this operation on another sheet. To combat this one, I made a worksheet object and qualified all of the ranges mentioned just to be sure that excel knows what location you're talking about.

An (unlikely based on your description, but possible) problem could be with the way that you specify the lastrow . If you have a column of contiguous values (although your description would suggest you do not) and you specify the last row of that column with a value, the .End(xlUp) will actually trace back up to the last cell with no value which could be the first row - so the loop never even runs! You could avoid this just by changing it to lastrow = Range("G582").End(xlUp).Row (one row below what you know to be the last full row), but why bother when you can just let excel do the work for you like in my answer below.

Sub cleanup()

    Dim ws As Worksheet
    Set ws = sheets("Sheet1")

    Dim lastRow As Long
    lastRow = ws.Range("G" & rows.count).End(xlUp).row

    Dim i As Long
    For i = 2 To lastRow
        If Len(ws.Cells(i, 1)) > 13 Then
            ws.Cells(i, 1) = Left(ws.Cells(i, 1), 13)
        End If
    Next i

End Sub

I also made a version with the second option that you mentioned in the question about splitting each row by a comma "," .

Sub cleanup2()

    Dim ws As Worksheet
    Set ws = sheets("Sheet1")

    Dim lastRow As Long
    lastRow = ws.Range("G" & rows.count).End(xlUp).row

    Dim i As Long
    For i = 2 To lastRow
        ws.Cells(i, 1) = Split(ws.Cells(i, 1), ",")(0)    'option 2
    Next i

End Sub

Hope this solves your problem!

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