Vba 通过查找匹配的键值更新值

Vba 通过查找匹配的键值更新值,vba,excel,Vba,Excel,这对你们中的许多人来说可能很容易,但我是VBA新手; 下面是我将多个Excel文件值添加到一个母版图纸中的代码。在执行此操作时,我希望更新有关其键值的值 Sub tekSheetMerging() Dim masterSheet As Worksheet Set masterSheet = sheets("KoMKo") 'Variable to save the used Range of the master sheet Dim usedRangeMaster As Integer

这对你们中的许多人来说可能很容易,但我是VBA新手; 下面是我将多个Excel文件值添加到一个母版图纸中的代码。在执行此操作时,我希望更新有关其键值的值

Sub tekSheetMerging()
Dim masterSheet As Worksheet

Set masterSheet = sheets("KoMKo")

'Variable to save the used Range of the master sheet
Dim usedRangeMaster As Integer

     Dim ws As Worksheet

     'loop through each worksheet in current workbook
    For Each ws In Worksheets

    'If sheetname contains "data" (UCase casts the Name to upper case letters)
    If InStr(1, UCase(ws.Name), "DATA", vbTextCompare) > 0 Then

        'calculate the used range of the master sheet
        usedRangeMaster = masterSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1

        'Variable to save the used Range of the sub sheet
        Dim usedRangeSub As Integer

        'calculate the used range of the sub sheet
        usedRangeSub = ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row

        'copy relevant range from the subsheet
        ws.Range("C1:C" & usedRangeSub).Copy

        'paste the copied range after the used range in column a
        masterSheet.Range("A" & usedRangeMaster).PasteSpecial
    End If
     Next ws

    End Sub
所以,现在我有一个Excel表,它在a列中包含一个键值,在该行的其他列中包含其他值。当我发现一个键与之前添加的键值重复时,我想删除该单元格及其完整的行。通过这样做,我想更新有关其键值的值。如果我再添加一个键,它应该会留在列表中,不会有任何问题


如何才能做到这一点?

最好先检查子表(源)中的键,然后将它们逐一与主表中的键进行比较。任何匹配项都应从主服务器中删除。然后将整个数据从子图纸复制到主图纸中

因此,我的代码位于这一行之前:

'copy relevant range from the subsheet
ws.Range("C1:C" & usedRangeSub).Copy
用您的变量替换我的变量:

With mastersheet
    for i = firstrow to lastrow   'first and lastrow of data in subsheet
        set f = .Range("A:A").Find(ws.Range("A" & i).Value, _
                LookIn:=xlValues, LookAt:=xlWhole)
        If Not f is Nothing then
            .Range("A" & f.Row).EntireRow.Delete
        End If
    next i
end with
不要忘记在此之后更新usedRangeMaster,因为它会因为删除而变短。然后复制粘贴数据

注意:此代码假定您的密钥是唯一的,并且您的主密钥中永远不会有重复的密钥(如果主密钥仅通过此代码编写,则会出现这种情况)。这就是为什么它只搜索一次主机


编辑:我看到那个怪物已经给你提供了一些链接了?无论如何,让我知道它是否有效。

最好先检查子表(源)中的键,然后将它们逐个与主表中的键进行比较。任何匹配项都应从主服务器中删除。然后将整个数据从子图纸复制到主图纸中

因此,我的代码位于这一行之前:

'copy relevant range from the subsheet
ws.Range("C1:C" & usedRangeSub).Copy
用您的变量替换我的变量:

With mastersheet
    for i = firstrow to lastrow   'first and lastrow of data in subsheet
        set f = .Range("A:A").Find(ws.Range("A" & i).Value, _
                LookIn:=xlValues, LookAt:=xlWhole)
        If Not f is Nothing then
            .Range("A" & f.Row).EntireRow.Delete
        End If
    next i
end with
不要忘记在此之后更新usedRangeMaster,因为它会因为删除而变短。然后复制粘贴数据

注意:此代码假定您的密钥是唯一的,并且您的主密钥中永远不会有重复的密钥(如果主密钥仅通过此代码编写,则会出现这种情况)。这就是为什么它只搜索一次主机


编辑:我看到那个怪物已经给你提供了一些链接了?无论如何,让我知道它是否有效。

以上代码是否有效,或者您是否收到错误?如果是,请告诉我们错误代码/消息以及错误发生在哪一行。如果代码运行时没有错误,那么请详细说明(1)代码的当前结果是什么,(2)上述代码的预期结果是什么(以及它们之间的区别)。目前,我在你的代码中只看到了
.Copy
.PasteSpecial
,我想知道你想如何实现你在文章中提到的
.Delete
。上面的代码是有效的还是有错误?如果是,请告诉我们错误代码/消息以及错误发生在哪一行。如果代码运行时没有错误,那么请详细说明(1)代码的当前结果是什么,(2)上述代码的预期结果是什么(以及它们之间的区别)。目前我在你的代码中只看到了
.Copy
.PasteSpecial
,我想知道你想如何实现你在文章中所说的
。Delete
。如果这对你有效,请接受它作为答案。谢谢这对你有效,请接受它作为答案。谢谢