简体   繁体   中英

Excel VBA - Append cell by removing string

I am looking to append cells containing numbers and text so that the text is removed.

Take 1.1 (Very Low) as an example of what is in the cell.

I would like to remove (Very Low) from the cell.

Use left and find :

=LEFT(A1,FIND("(",A1,1)-1)

在此处输入图像描述

VBA:

Dim p As String
Dim loc
p = "("

With ActiveSheet
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

For i = 1 To lastrow
    loc = InStr(1, .Cells(i, "A").Value, p)
    .Cells(i, "A") = Left(.Cells(i, "A"), loc - 1)
Next

End With

I have figured out how to do this using InStr .

I was trying to edit cells in a range by cycling through them whilst checking for a string. I would then remove the specified string. You can do this by checking the cell for your string using InStr and then using Replace to update the cell contents.

I found a way to do this using:

Dim ws as Worksheet, rCell As Range, rArea As Range

    Set rArea = ws.Range("A1:A10")
    For Each rCell In rArea.Cells
        If InStr(rCell.Value, " (Very Low)") Then
            rCell.Value = Replace(rCell.Value, " (Very Low)", "")
        End If
    Next rCell

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