使用新值更新表

时间:2015-06-01 20:49:49

标签: excel vba

我正在尝试使用唯一索引列更新excel中的数据表,其中第二个表中的新值也使用宏使用相同的唯一索引列。我是VB编程的新手,所以我试着寻找一个解决方案,我想我发现了一些应该有用的东西,但是没有。我确定这是我搞砸的事情。

   Sub LookupAndPaste()
Dim RLookup As Long, endrowRLookup As Long
Dim r As Long, endRow As Long

endrowRLookup = Sheets("Exp data").Cells(3, 44).Value
endRow = Range("count_exp_data").Value

For RLookup = Sheets("Exp data").Cells(2, Columns("AT").Column) To Sheets("Exp data").Cells(endrowRLookup, Columns("AT").Column)
'Copy the current row
Range(Cells(RLookup, Columns("AT").Column), Cells(RLookup, Columns("CJ").Column)).Select
     Selection.Copy
    For r = 1 To endRow
        If Sheets("Exp data").Cells(r, Columns("A").Column).Value = RLookup Then 'Found
            Sheets("Exp data").Cells(r, Columns("A").Column).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

        End If
    Next r
Next RLookup
End Sub

目标搜索表位于A到AO列。包含要更新数据的行列表在AT到CJ中,并且是大小为1到100行的任何行。

当代码执行时,我在复制模式下以AT100:CJ100结束,没有任何改变。

由于

1 个答案:

答案 0 :(得分:0)

解决我的问题是我第一次使用For

Sub LookupAndPaste()
Dim RLookup As Long, endrowRLookup As Long
Dim r As Long, endRow As Long

endrowRLookup = Sheets("Exp data").Cells(3, 44).Value
endRow = Range("count_exp_data").Value

For RLookup = 1 To endrowRLookup
'Copy the current row
Range(Cells(RLookup + 1, Columns("AT").Column), Cells(RLookup + 1, Columns("CJ").Column)).Select
     Selection.Copy
    For r = 1 To endRow
        If Sheets("Exp data").Cells(r, Columns("A").Column).Value = Cells(RLookup + 1, Columns("AT").Column).Value Then 'Found
            Sheets("Exp data").Cells(r, Columns("A").Column).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

        End If
    Next r
Next RLookup
End Sub