1

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

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

任何帮助,将不胜感激!

4

1 回答 1

0

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

当你看到Invalid outside procedure你通常有不在和之间的代码SubEnd Sub

该语句必须出现在 Sub 或 Function 或属性过程(Property Get、Property Let、Property Set)中。

此外,您应该检查您的代码是否在 a 中Module而不是在worksheetorworkbook代码中

于 2013-10-28T15:13:29.837 回答