如何将分隔的字符串拆分为特定的单个单元格?

时间:2017-03-24 14:24:47

标签: excel vba excel-vba

我有以下节点:

enter image description here

预期输出:

enter image description here

我目前的步骤是:

  1. 以“/”

  2. 分隔
  3. 排序

  4. 有条件地格式化一些单元格。我正在尝试通过执行A2 = A1并将字体颜色设置为白色来使单元格变为空白。但这不起作用。

  5. enter image description here

    如何使用VBA执行此操作?我被告知这可能需要VBA。 诀窍是每行只保留一个父节点。

2 个答案:

答案 0 :(得分:1)

尝试使用公式(在新工作表或列中),如

X2     =IF(A2=A1,"",A2)
X3     =IF(A3=A2,"",A3)
X4     =IF(A4=A3,"",A4)

等。

对于A栏

答案 1 :(得分:-1)

快速而肮脏,但它完成了工作。这将循环遍历列A-F第1-12行并选择每个单元格的值查找该出现并删除第二个出现而不是第一个出现。这应该可以解决问题。

Sub findItemInColumns()

    With ThisWorkbook.Worksheets("Sheet1")
        For r = 1 To 12
            For c = 1 To 6
                RemoveDups .Cells(r, c).Value
            Next
        Next
    End With
End Sub

Sub RemoveDups(ByVal somevalueToFindAndRemove As String)

    Dim FindString As String
    Dim Rng As Range



    listOfValues = Array(somevalueToFindAndRemove)

    If Trim(somevalueToFindAndRemove) <> "" Then
        With Sheets("Sheet1").Range("A:AK")

            For i = LBound(listOfValues) To UBound(listOfValues)

                Set Rng = .Find(What:=listOfValues(i), _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing Then 
                    FirstAddress = Rng.Address
                    Do
                        Application.Goto Rng, True    
                        If Rng.Address <> FirstAddress Then Rng.Value = ""
                        Set Rng = .FindNext(Rng)
                    Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
                End If
            Next i
        End With
    End If

End Sub