![](/img/trans.png)
[英]Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match
[英]Excel Macro Copy cell range & paste data one sheet to another
我正在嘗試查找給定列表的值,並將剩余的行內容復制到與列表相鄰的列中。 我從該站點復制了一些代碼,並在其他地方找不到答案。 每次我運行代碼時,都會給我一個編譯錯誤“無效的外部過程”
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
任何幫助,將不勝感激!
您提交的代碼可以編譯並正常運行。
當您看到Invalid outside procedure
您通常會有不在Sub
和End Sub
之間的代碼
該語句必須出現在Sub或Function或屬性過程(Property Get,Property Let,Property Set)中。
另外,您應該檢查代碼是否在Module
,而不在worksheet
或workbook
代碼中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.