【问题标题】:Conflicting DATA - Duplicated values数据冲突 - 重复值
【发布时间】:2018-04-12 15:39:29
【问题描述】:

我创建了一个宏,用另一张表中的特定数据填充缺失的数据,这些代码在从客户端的 excel 复制粘贴数据并准备开始工作所需的数据时完美工作,但下面唯一的问题是

代码:

   With Worksheets("Feuil2") 
   ' reference "target" sheet (change "Target" to our actual target sheet name)
     With .Range("B1:B" & .Cells(.Rows.Count, 1).End(xlUp).Row) 'reference 
         its column B range from row 1 down to last not empty one
        If WorksheetFunction.CountBlank(.Cells) > 0 Then 
 ' if any blank cell in referenced range. this check to avoid error thrown by subsequent 
    statament
            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=index(GDC!" & rng.Offset(, 1).Address(, , xlR1C1) & ",match(RC[-1],GDC!" & rng.Address(, , xlR1C1) & ",0))" 'fill blank cells with a lookup (well, sort of) formula
            .Value = .Value 'get rid of formulas and leave values only
            Cells.Select
        End If
    End With
End With

这段代码在匹配和填充数据方面工作得很好,但是当例如找到一个重复的值时,它只复制第一个值而不是第二个

请参阅下图以更好地了解主要问题:

正如您在图像中看到的那样,在 A 列中,我可能有数据重复两次,如该值 P20845,在 F 列中重复一个名称为 Ghaith,另一个名称为 sirine,但正如您可以在 A 列中看到它也只是 Ghaith 的名字,没有 sirine 的名字 任何想法或更好的解决方案来解决这个问题并获得所有需要的数据? .

最好的问候

波洛斯

【问题讨论】:

  • 我已经多次阅读这个问题,但我仍然无法告诉 什么 您的确切问题是什么。你能再解释一下吗?
  • @dwirony 谢谢你的回答,在图像中有一个值P20845 column A with the name of Gaith in front of it 我想要的是当我运行我的代码并发现重复的值时,它会将结果都放在前面第一个数据和第二个像这样 column B the value P20845 is repeated twice one with name of ghaith and one with the name of sirine 所以有什么办法可以进入 A 列 in front of P20845 something like this Ghiath/sirine 查看图片

标签: vba excel


【解决方案1】:

或者使用字典

Option Explicit

Public Sub AddValues()
    Application.ScreenUpdating = False
    Dim wb As Workbook, wsSource As Worksheet, wsTarget As Worksheet, masterDict As Object, arr() As Variant, i As Long, rng As Range
    Set wb = ThisWorkbook
    Set wsSource = wb.Worksheets("Feuil1")
    Set wsTarget = wb.Worksheets("Feuil2")
    Set masterDict = CreateObject("Scripting.Dictionary")

    With wsSource
        arr = Intersect(.Columns("A:B"), .UsedRange)
        For i = 1 To UBound(arr, 1)
            If Not masterDict.exists(arr(i, 1)) Then masterDict.Add arr(i, 1), GetAllMatches(arr(i, 1), arr(i, 2), wsSource)
        Next i
    End With

    With wsTarget
        For Each rng In Intersect(.Columns("A"), .UsedRange)
            On Error Resume Next
            rng.Offset(, 1) = masterDict(rng.Value)
            On Error GoTo 0
        Next rng       
    End With
    Application.ScreenUpdating = True
End Sub
Public Function GetAllMatches(ByVal findString As String, ByVal dupString As String, ByVal searchRng As Worksheet) As String

    Dim foundCell As Range
    Dim concatenatedString As String
    concatenatedString = vbNullString

    With Intersect(searchRng.Columns(1), searchRng.UsedRange)

        Set foundCell = .Find(findString)
        If foundCell Is Nothing Then Exit Function
        If Not foundCell Is Nothing Then concatenatedString = foundCell.Offset(, 1)

        Dim currMatch As Long
        currMatch = 0

        For currMatch = 1 To WorksheetFunction.CountIf(.Cells, findString)

            Set foundCell = .Find(What:=findString, After:=foundCell, _
                                  LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                  SearchDirection:=xlNext, MatchCase:=False)

            If Not foundCell Is Nothing And InStr(1, dupString, concatenatedString) = 0 Then
                concatenatedString = concatenatedString & "/" & foundCell.Offset(, 1)
            Else
                concatenatedString = foundCell.Offset(, 1)
            End If
        Next currMatch
    End With
    GetAllMatches = concatenatedString
End Function

Feuil2 中的输出

【讨论】:

  • 感谢您的回答我正在测试您的代码,但在运行它时遇到了问题,它一直在不停地循环...我可以与您分享我的文件吗?坦克你这么多
  • 所以我的代码中的 sheet2 将是你的左图。您可以将 Intersect(.Columns("F"), .UsedRange) 替换为 .Range("F1:F7") 并且 Sheet1 是您的左侧图像,您可以将 Intersect(.Columns("A"), .UsedRange) 替换为.Range("A1:A6.")。我已经编辑了您将放置这些替换的位置。告诉我。
  • 请问最后一个问题,我可以在 C 列中将其修改为 foundCell.Offset(, 2) ???
  • 你想做什么?
  • 抱歉,我尝试制作另一列像 imgur.com/Crhavyx Feuil1 和我想要的也是在 Feuil2 但在另一列中获取这些数据:) :-)
【解决方案2】:

也许是这样的?

Sub Test()

Dim i As Long, myrow As Long, lastrow As Long
Dim sht1 As Worksheet, sht2 As Worksheet

Set sht1 = ThisWorkbook.Worksheets("Sheet1")
Set sht2 = ThisWorkbook.Worksheets("Sheet2")

lastrow = 1

For i = 1 To 7
    If Application.WorksheetFunction.CountIf(sht1.Range("A:A"), sht2.Range("F" & i).Value) = 0 Then
        If i = 1 Then
            lastrow = 1
        Else
            lastrow = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row + 1
        End If

        sht1.Range("A" & lastrow).Value = sht2.Range("F" & i).Value
        sht1.Range("B" & lastrow).Value = sht2.Range("G" & i).Value
    Else
        sht1.Range("B" & sht1.Columns("A:A").Find(What:=sht2.Range("F" & i).Value).Row).Value = sht1.Range("B" & sht1.Columns("A:A").Find(What:=sht2.Range("F" & i).Value).Row).Value & "/" & sht2.Range("G" & i).Value
    End If
Next i

End Sub

【讨论】:

  • @diwrony ,谢谢 是否可以修改工作表,例如 Sheet1 中的 A 列和 Sheet2 中的 F 列???
  • @POLOSTutorials 更新
  • 我测试了你的代码,但出了点问题,它正在复制位于 sheet1 中的内容,因为在你更新答案之前它已经工作了,而缺少的只是在 sheet1 中的表 ColumnAColumnF 之间发生变化如果你愿意,我可以在表 2 中与你分享我的文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多