在另一个工作簿中运行时,一个工作簿中的工作宏抛出错误1004

时间:2014-02-26 22:44:32

标签: excel vba excel-vba

我在一个工作簿中的一个宏中收到Run-time error '1004' - Method 'Range' of object'_Global' failed消息,并且正在运行相同的宏另一个成功运行的工作簿。我缺少一个潜在的要求吗?经过对一些问答网站的大量研究后,我无法确定VBA代码超出范围的位置。

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

这是不起作用的宏。看看我在星号之间的评论。

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

感谢您提出的任何建议。

1 个答案:

答案 0 :(得分:2)

是:

 Range("A" & currentlastrow).Select  

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

(使用“&”代替“,”