Excel 循环分割值单元格并替换

Excel 循环分割值单元格并替换,excel,vba,foreach,split,str-replace,Excel,Vba,Foreach,Split,Str Replace,我在Excel中创建了一个宏。在A列中,值用分号分隔 有一个循环通过a列中的拆分值替换B列中的拆分值 循环分割值不起作用 Sub ReplaceAttachments3() Dim cl As Range Dim cell As Variant Dim i As Long Dim txt As String For Each cl In Range("$B$1:$B" & Range("$B65536").End(xlUp).Row) txt = Cells(cl.Row, 1)

我在Excel中创建了一个宏。在A列中,值用分号分隔

有一个循环通过a列中的拆分值替换B列中的拆分值

循环分割值不起作用

Sub ReplaceAttachments3()
Dim cl As Range
Dim cell As Variant
Dim i As Long
Dim txt As String
For Each cl In Range("$B$1:$B" & Range("$B65536").End(xlUp).Row)
    txt = Cells(cl.Row, 1)
    cell = split(txt, ";")
    For i = 0 To UBound(cell)
        Cells(cl.Row, 2).replace What:=txt,   Replacement:="",LookAt:=xlPart, SearchOrder:= _
            xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        Application.Goto Reference:="ReplaceAttachments"
    Next i
Next
End Sub

这就是你想要的吗?注意:您不需要选择完整的列。只需找到最后一行并仅使用该范围:)

我已经对代码进行了注释,所以您在理解它时应该不会有问题。但如果你这样做了,那么只需发回

Sub Sample()
    Dim ws As Worksheet
    Dim aCell As Range, rng As Range
    Dim Lrow As Long, i As Long
    Dim MyAr

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find the last row in Col A
        Lrow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Set your range
        Set rng = .Range("A1:A" & Lrow)

        '~~> Loop trhough your range
        For Each aCell In rng
            '~~> Skip the row if value in cell A is blank
            If Len(Trim(aCell.Value)) <> 0 Then
                '~~> Check if the cell has ";"
                '~~> If it has ";" then split and replace else
                '~~> Replace without splitting
                If InStr(1, aCell.Value, ";") Then
                    MyAr = Split(aCell.Value, ";")

                    For i = LBound(MyAr) To UBound(MyAr)
                        aCell.Offset(, 1).Value = Replace(aCell.Offset(, 1).Value, Trim(MyAr(i)), "")
                    Next i
                Else
                    aCell.Offset(, 1).Value = Replace(aCell.Offset(, 1).Value, Trim(aCell.Value), "")
                End If
            End If
        Next
    End With
End Sub
子样本()
将ws设置为工作表
调暗aCell作为范围,rng作为范围
朦胧如长,我如长
暗MyAr
“~~>将此更改为相关工作表
设置ws=ThisWorkbook.Sheets(“Sheet1”)
与ws
“~~>查找列A中的最后一行
Lrow=.Range(“A”&.Rows.Count).End(xlUp).Row
“~~>设置您的范围
设置rng=.Range(“A1:A”和Lrow)
“~~>在您的范围内循环
对于rng中的每个aCell
“~~>如果单元格A中的值为空,则跳过该行
如果Len(Trim(aCell.Value))为0,则
“~~>检查单元格是否有”
“~~>如果有”;“则拆分并替换else
“~~>替换而不拆分
如果InStr(1,aCell.Value,“;”),则
MyAr=拆分(aCell.Value,“;”)
对于i=LBound(MyAr)到UBound(MyAr)
aCell.Offset(,1).Value=Replace(aCell.Offset(,1).Value,Trim(MyAr(i)),“”)
接下来我
其他的
aCell.Offset(,1).Value=Replace(aCell.Offset(,1).Value,Trim(aCell.Value),“”)
如果结束
如果结束
下一个
以
端接头
屏幕截图:


为什么不简单地使用
文本到列
?不需要VBA。如果您仍然需要VBA,则为
文本记录一个宏到列
我想替换B列中的拆分值。例如,在A列中:“A;b;c;”b列:“文本A文本2 b文本2 c”,在b列中,我希望替换空值上的A、b、c等值。因此,是否要从b列中删除拆分值?是的,我希望从b列中删除te拆分值,并且此拆分值在A列中以分号分隔确定。我已经贴出了答案。您可能需要刷新页面才能看到它。希望这就是你想要的?