简体   繁体   English

如果目标值已存在,请替换数据范围

[英]Replace range of data if target value already exists

The following script selects a range of data on one sheet and transfers the selection to another sheet. 以下脚本在一张纸上选择一系列数据,然后将所选内容转移到另一张纸上。

LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow

    If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
        Range(Cells(i, 1), Cells(i, 4)).Select
        Selection.Copy

        erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues

        If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
        If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
        If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
        ActiveWorkbook.Save

    End If
Next i

I would now like to introduce a script which will replace the row of data on the target sheet if the value in column A already exists, but i'm not sure how to achieve this, any help is much appreciated. 我现在想介绍一个脚本,如果A列中的值已经存在,它将替换目标工作表上的数据行,但是我不确定如何实现这一点,我们将不胜感激。

Thank you in advance. 先感谢您。

Public Function IsIn(li, Val) As Boolean
    IsIn = False
    Dim c
    For Each c In li
        If c = Val Then
            IsIn = True
            Exit Function
        End If
    Next c
End Function

dim a: a= range(destWB.sheet(whatever)..range("A1"),destWB.Range("A" & destWB.sheet(whatever).Rows.Count).End(xlUp)).value
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
    if isin(a, Cells(i, 1) ) then
    do whatever you want
    else
    If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
        Range(Cells(i, 1), Cells(i, 4)).Select
        Selection.Copy

        erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues

        If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
        If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
        If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
        ActiveWorkbook.save
    End If    
    End If
Next i

I suggest using a Dictionary -Object which is most likely a Hash-Map . 我建议使用Dictionary- Object,它很可能是Hash-Map The advantage is that you can use the built in method Dictionary.Exists(Key) to check if the Dictionary already holds the specified value (Key). 优点是您可以使用内置方法Dictionary.Exists(Key)来检查Dictionary是否已保存指定的值(Key)。

Also you should not save the Workbook in every step of the iteration. 另外,您不应该在迭代的每个步骤中保存工作簿。 It would be better (and faster) to only save the workbook after completing the copying of your whole data. 最好在完成整个数据的复制后才保存工作簿(并且更快)。

Additionally your If -Tests after copy-paste are not neccessary, because you are already checking for Cells(i,1)<>"" before copying so you don't have to check this again as it does not change. 另外,不需要复制后的If -Test,因为在复制之前您已经在检查Cells(i,1)<>"" ,所以您不必再次检查它,因为它不会更改。

The following code shows how to get your desired result: 以下代码显示了如何获得所需的结果:

Set dict = CreateObject("Scripting.Dictionary")
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow

  If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then

    If dict.Exists(Cells(i,1).Value) Then
    'value already exists -> update row number
      dict.Item(Cells(i,1).Value)=i
    Else
    'save value of column A and row number in dictionary
      dict.Add Cells(i,1).Value, i
    End If

    Cells(i, 22).Value = "Yes"
    Cells(i, 23).Value = Now
    Cells(i, 24).Value = Environ("UserName")

  End If
Next i

'finally copy over your data (only unique values)
For Each i In dict.Items
    Range(Cells(i, 1), Cells(i, 4)).Select
    Selection.Copy

    erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
Next i

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

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