Excel宏复制单元格范围&将数据粘贴到另一张

时间:2013-10-28 15:09:36

标签: excel vba excel-vba

我正在尝试查找给定列表的值,并将剩余的行内容复制到列表旁边的列中。我从这个网站复制了一些代码,并在其他地方寻找答案找不到任何东西。每次运行代码时,它都会给我一个编译错误“Invalid outside procedure”

Sub test()
Dim LR As Long
Dim i As Long
Dim x As Long
Dim lastDataRow As Long
Dim lastListRow As Long
Dim sheetOne As String
Dim sheetTwo As String

Dim listItem As String
Dim dataItem As String

Dim listColNum As Long
Dim dataColNum As Long

listColNum = 1
dataColNum = 2
sheetOne = "new_copy"
sheetTwo = "Historical_data_"

lastListRow = Sheets(sheetOne).Cells(Sheets(sheetOne).Rows.Count, listColNum).End(xlUp).row
lastDataRow = Sheets(sheetTwo).Cells(Sheets(sheetTwo).Rows.Count, dataColNum).End(xlUp).row

For x = 1 To lastListRow

    For i = 1 To lastDataRow

        If Sheets(sheetOne).Cells(x, listColNum).Value = Sheets(sheetTwo).Cells(i, dataColNum).Value Then
            Sheets(sheetOne).Cells(x, 3).Value = Sheets(sheetTwo).Cells(i, 3).Value
        End If

    Next i
Next x
End Sub

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您提交的代码编译并运行时没有问题。

当您看到Invalid outside procedure时,您的代码通常不在SubEnd Sub之间

  

声明必须在Sub或Function或属性中发生   程序(Property Get,Property Let,Property Set)。

此外,您应该检查您的代码是Module而不是worksheetworkbook代码