在另一个工作簿中运行时,在一个工作簿中工作macros引发错误1004

我正在接收Run-time error '1004' - Method 'Range' of object'_Global' failed一个工作簿中的一个macros中Run-time error '1004' - Method 'Range' of object'_Global' failed消息的Run-time error '1004' - Method 'Range' of object'_Global' failed ,并运行成功运行的同一个macros另一个工作簿。 是否有一个基本的要求,我失踪了? 在多个问答网站进行了大量的研究之后,我无法确定VBA代码在哪里出了问题。

我试图从Tab1( Workforce Detail )复制数据到Tab2( Current )从Tab1不同的select。 每次macros从Tab1中select更多的数据时,我想复制并粘贴到Tab2中的第一个打开的行。 指定活动单元格以在第二次select数据后开始粘贴时收到错误。

这是不工作的macros。 看到我之间评论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 

感谢您的任何build议。

它是:

  Range("A" & currentlastrow).Select 

 Range("A", Cells(currentlastrow)).Select 

(使用“&”代替“,”