从一个工作表复制和粘贴到同一活动工作簿中的另一个工作表时出现错误1004

时间:2019-07-12 05:10:31

标签: excel vba

我是一名Excel VBA新手,试图从多个工作表中复制一定范围的单元格,我想通过查找最后一个空行并将此内容粘贴到同一工作簿中的另一个工作表上,并将其粘贴到每个工作表中工作表。

执行代码时,出现1004错误。我尝试测试代码的不同部分,并注意到当我尝试在工作表中选择一个单元格时,也会遇到相同的1004错误,但现在显示以下“ Range类的select方法错误”。

我尝试使用不同的方法在工作表中选择此单元格,例如带有“ Sheets”和“ Worksheets”的“ ThisWorkbook”,“ ActiveWorkbook”,并且由于某种原因,我遇到相同的错误。

该代码在模块中,我使用一个按钮调用该代码的执行。

有趣的是,当我先选择工作表然后选择范围时,我没有遇到这个问题。

赞:

ThisWorkbook.Sheets(3).Select
ThisWorkbook.Sheets(3).Range("A6").Select

我想了解为什么会这样,所以我可以避免在代码中使用select。

顺便说一句,英语不是我的母语,以防万一我没有翻译Visual Basic正确显示的错误。

这是我的代码:

Sub ConsolidacionReportes()

Dim Fila As Long   ' to count each row in a for loop
Dim UltimaFila As Integer  ' Used to find the last row of a sheet
Dim Hojas As Integer ' used in a for loop to go through each sheet in the workbook
Dim RangoCeldas As String ' Range of cells
Dim BorrarTotales As Long ' another variable to count rows

For Hojas = 2 To 6

    UltimaFila = Sheets(Hojas).Cells(Sheets(Hojas).Cells.Rows.Count, "C").End(xlUp).Row - 1

    For Fila = UltimaFila To 5 Step -1
        RangoCeldas = "C"
        If Sheets(Hojas).Cells(Fila, "C") = "" Then
            Sheets(Hojas).Rows(Fila).Delete
        End If
    Next

Next

For Hojas = 2 To 6

BorrarTotales = Sheets(Hojas).Cells(5, 3).End(xlDown).Row + 3

     For Fila = BorrarTotales To 5 Step -1
        If Sheets(Hojas).Cells(Fila, "C") = "" Then
            Sheets(Hojas).Rows(Fila).Delete
        End If
    Next

Next

Reporte1.Range("A1:I5").Copy Calculos.Range("A1")

' When trying to select this range I get the error 1004: Error in select method of Range class

ThisWorkbook.Sheets(3).Range("A6").Select

For Hojas = 2 To 6

    UltimaFila = Sheets(Hojas).Cells(5, 1).End(xlDown).Row

    Let RangoCeldas = "A6:" & "B" & UltimaFila

' When executing the code I get another error 1004, this time the description is: Error defined by the application or the object

    Sheets(Hojas).Range(RangoCeldas).Copy Calculos.Cells(Calculos.Cells(5, 3).End(xlDown), 1)

Next

End Sub

1 个答案:

答案 0 :(得分:0)

对不起,时间有点短,所以无法完全检查。此代码应该使您更接近解决问题。

Sub Test()

    Dim wrkSht As Worksheet 'Will hold a reference to the worksheet being copied from.
    Dim Fila As Long
    Dim UltimaFila As Long

    'Cycle through each worksheet, storing a reference in wrkSht.
    For Each wrkSht In ThisWorkbook.Worksheets
        With wrkSht
            'Check the codename of the sheet.
            'If it's the sheet being copied to ignore it, otherwise process it.
            'Not sure what Reporte1 is, but also ignoring it.
            Select Case .CodeName
                Case "Calculos", "Reporte1"
                    'Do nothing.
                Case Else
                    'Delete rows that have a blank column C.
                    'Could use a filter to speed this step up.
                    If UltimaFila >= 5 Then
                        For Fila = UltimaFila To 5 Step -1
                            If .Cells(UltimaFila, 3) = "" Then
                                .Rows(Fila).Delete
                            End If
                        Next Fila
                    End If

                    'Copy the range from wrksht to Calculos
                    .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Copy _
                        Destination:=Calculos.Cells(.Rows.Count, 1).End(xlUp)

            End Select
        End With
    Next wrkSht

End Sub