简体   繁体   English

Excel VBA - 通过删除字符串附加单元格

[英]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.1.1 (Very Low)为例说明单元格中的内容。

I would like to remove (Very Low) from the cell.我想从单元格中删除(Very Low)

Use left and find :使用leftfind

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

在此处输入图像描述

VBA: 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 .我已经想出了如何使用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.为此,您可以使用InStr检查单元格中的字符串,然后使用Replace更新单元格内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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