我正在一個工作簿中的一個宏中收到Run-time error '1004' - Method 'Range' of object'_Global' failed
消息,並且正在運行成功運行的另一個工作簿。是否有我缺少的基本要求?經過對許多問題和答案網站的大量研究後,我無法確定VBA代碼超出範圍。在一個工作簿中工作宏引發錯誤1004在另一個工作簿中運行時
我試圖從Tab1(Workforce Detail
)複製數據到Tab2(Current
)與Tab1不同的選擇。每次宏從Tab1選擇更多的數據時,我想複製並粘貼到Tab2中的第一個打開的行。指定活動單元格以在第二次選擇數據後開始粘貼時收到錯誤。
這是宏不起作用。看到我的評論astericks之間。
Sub SelectJobCode()
'
' SelectJobCode Macro
'
'
Dim lastrow, currentlastrow As Long
*** Clears the "Current" sheet (Tab2) in the workbook - no issues ***
Sheets("Current").Select
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
With ActiveSheet
currentlastrow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
End With
If currentlastrow > 1 Then
Range("A2", Cells(currentlastrow, "ap")).Select
Selection.ClearContents
End If
Range("A2").Select
*** Goes to Workforce Detail sheet/tab (Tab1) to filter specific columns. No issues ***
Sheets("GHR-77025 Workforce Detail Repo").Select
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
With ActiveSheet
lastrow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
End With
If lastrow > 1 Then
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("GHR-77025 Workforce Detail Repo").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("GHR-77025 Workforce Detail Repo").Sort.SortFields.Add Key:= _
Range(Cells(2, "g"), Cells(lastrow, "g")), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("GHR-77025 Workforce Detail Repo").Sort
.SetRange Range("A2", Cells(lastrow, "ap"))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
*** Column 7 is Job Code. Filter for Job Codes = "CA600" and "CA601". No Issues.***
ActiveSheet.Range("A2", Cells(lastrow, "ap")).AutoFilter Field:=7, Criteria1:=Array(_
"CA600", "CA601"), Operator:=xlFilterValues
*** Determine row count in Tab1, select filtered data, go to "Current" sheet (Tab2), paste data beginning in cell A2. No issues. ***
With ActiveSheet
lastrow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
End With
If lastrow > 1 Then
ActiveSheet.Range("A2", Cells(lastrow, "ap")).Select
Selection.Copy
Sheets("Current").Select
Range("A2").Select
ActiveSheet.Paste
Range("A2").Select
End If
***Go back to Tab1. Clear filters. Refilter on 2 more job codes. Determine the last row of filter data, select range to copy. No issues ****
Sheets("GHR-77025 Workforce Detail Repo").Select
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
Range("A2").Select
ActiveSheet.Range("A2", Cells(lastrow, "ap")).AutoFilter Field:=7, Criteria1:=Array(_
"OK101", "OK102"), Operator:=xlFilterValues
' "OK111", "OK112", "OK202", "OK205", "OK206", "OK207" _
' , "OK212", "OK314", "OK316", "SR007", "SR030", "YZ020"), Operator:=xlFilterValues
With ActiveSheet
lastrow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
End With
*** Select range of filtered data on Tab1 to prepare to paste to Tab2. No issues. ***
If lastrow > 1 Then
ActiveSheet.Range("A2", Cells(lastrow, "ap")).Select
Selection.Copy
*** Go to Tab2 "Current". Determine the last row. Add 1 to the last row. No issues. ****
Sheets("Current").Select
Range("A2").Select
With ActiveSheet
currentlastrow = Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
End With
currentlastrow = currentlastrow + 1
*** The above code works. The value in currentlastrow is 256, exactly what I expect it to be. ***
MsgBox currentlastrow
*** ActiveSheet should still be "Current". During the trial and error phase (before giving up and submitting my question here), I have added code to specify the sheet, with no luck***
If currentlastrow > 1 Then
*** This next statement is where the error occurs. ***
*** This very same syntax runs successfully in another workbook. Is there some underlying cause specific to a given workbook? Can you see why this statement is receiving the error? What am I missing? ***
Range("A", Cells(currentlastrow)).Select
Else
Range("A2").Select
End If
ActiveSheet.Paste
Range("A2").Select
End If
Sheets("GHR-77025 Workforce Detail Repo").Select
If ActiveSheet.FilterMode = True Then
ActiveSheet.ShowAllData
End If
Range("A2").Select
MsgBox "Current tab formatted"
End Sub
感謝您的任何建議。
它的範圍( 「A」 **&** Cells(currentlastrow))選擇範圍(「A」**,** Cells(currentlastrow))。選擇我認爲 – Alex
Alex-應該這樣做,我會把它作爲答案。 – guitarthrower
@ user3357997:歡迎和很好的問題。很好的佈局和易於理解。 – guitarthrower