简体   繁体   English

Excel-查找并替换部分字符串但在同一单元格中?

[英]Excel - Find & Replace Part of String But in Same Cell?

I have 我有

Column A

Red-US
Blue-INT
Purple-INT
White-US-CA

Trying remove -us, int, ca, etc. 尝试删除-us, int, ca, etc.

So it's just Red, Blue, Purple, etc. 所以只有Red, Blue, Purple, etc.

Can't use Trim or Substitute formula because I want it to change directly in Column A (replace) 无法使用Trim或Substitute公式,因为我希望它直接在A列中更改(替换)

Thank you! 谢谢!

If the "-" is a consistent separator then it should be pretty simple. 如果“-”是一致的分隔符,则它应该非常简单。

Here are some commands you could use: 这是您可以使用的一些命令:

Strings and Manipulations 字符串和操作

Edit: Added simple code 编辑:添加了简单的代码

Sub textuptodash()
    i = 1 'start on row 1
    Do While Not IsEmpty(Cells(i, 1)) 'do until cell is empty
        If Not InStr(1, Cells(i, 1), "-") = 0 Then 'if dash in cell
            Cells(i, 1) = Left(Cells(i, 1), InStr(1, Cells(i, 1), "-") - 1) 'change cell contents
        End If
        i = i + 1 'increment row
    Loop
End Sub

Try using Split as follows: 尝试按以下方式使用Split

Sub MySplit()

    Dim rngMyRange As Range, rngCell As Range
    Dim strTemp() As String, strDel As String, strTest As String
    Dim lngCnt As Long
    Set rngMyRange = Range("A1:A4")

    For Each rngCell In rngMyRange
        ' Split the test based on the delimiter
        ' Store entries in a vector of strings
        strTemp = Split(rngCell.Text, "-")
        ' Reset cell value and intermmediate delimiter
        rngCell.Value = vbNullString
        strDel = vbNullString
        ' Scan all entries. store all of them but not the last -* part
        For lngCnt = LBound(strTemp) To UBound(strTemp) - 1
            rngCell = rngCell & strDel & strTemp(lngCnt)
            ' If we enter the loop again we will need to apend a "-"
            strDel = "-"
        Next lngCnt
    Next rngCell
End Sub

Output: 输出:

Red
Blue
Purple
White-US

It is not entirely clear how you want the last entry to be split: assumed that you want to remove the last "-*" bit only. 尚不清楚要如何拆分最后一个条目:假设只想删除最后一个“-*”位。 To keep the first part only, comment out the For lngCnt loop. 仅保留第一部分,请注释掉For lngCnt循环。

I hope this helps! 我希望这有帮助!

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

相关问题 Excel:在单元格中重新格式化/替换字符串的一部分 - Excel: reformat/replace part of string in cell Excel:有没有办法替换单元格字符串的特定部分? - Excel: is there a way to replace a peculiar part of a cell string? Excel:替换单元格的字符串值的一部分 - Excel: replace part of cell's string value vba excel - 在同一单元格内多次出现条件+时查找和替换 - vba excel - Find and replace on condition + within same cell multiple occurance 可以在同一个单元格中找到参数的位置并在excel中替换/多个吗? - It is possible to find position of parameters in same cell and replace/multiple in excel? 在excel单元格中提取字符串的一部分 - Extract part of a string in excel cell Excel宏替换部分字符串 - Excel macro to replace part of string 使用VBA宏在一个单元格中查找并替换其下面的单元格值替换字符串的一部分 - Find and replace part of string in one cell with value of cell below it with VBA Macro Powershell -> 在 Excel 中查找/替换 -> 在一个单元格中查找字符串并在另一个单元格中更改字符串 - Powershell -> find/replace in Excel -> look for a string in one cell and change the string in another cell 如何使用格式化文本查找和替换单元格的一部分 - How to find and replace part of a cell with formatted text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM