Application-defined or object-defined error 1004

后端 未结 1 370
青春惊慌失措
青春惊慌失措 2021-01-24 02:24

VBA is throwing the above given error on the line Sheets(\"Sheet1\").Range(\"A\" & i).Copy Destination:=Sheets(\"Sheet2\").Range(\"A\" & i & \"A\" & LastC

相关标签:
1条回答
  • 2021-01-24 03:12

    You are missing a ":" before "A"

    Range("A" & i & ":A" & LastCol - 1)
    

    FOLLOWUP

    After I went through your comments, I saw lot of errors in your code

    1) You have dimmed i as Integer. This can give you an error in Excel 2007 onwards if your last row is beyond 32,767. Change it to Long I would recommend having a look at this link.

    Topic: The Integer, Long, and Byte Data Types

    Link: http://msdn.microsoft.com/en-us/library/aa164754%28v=office.10%29.aspx

    Quote from the above link

    Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647

    2) You are finding the Last Column But in Which Sheet? You have to fully qualify the path Like this.

        If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
           LastCol = Cells.Find(What:="*", After:=[A1], _
           SearchOrder:=xlByColumns, _
           SearchDirection:=xlPrevious).Column
        End If
    

    Same is the case with

    With Sheets("Sheet1")
        Set DatesRange = Range("B2" & LastCol)
    End With
    

    You are missing a DOT before Range

    This is the correct way...

    .Range("B2....
    

    Also Range("B2" & LastCol) will not give you the range that you want. See the code below on how to create your range.

    3) You are using a variable LastColumn but using LastCol. I would strongly advise using Option Explicit I would also recommend having a look at this link (SEE POINT 2 in the link).

    Topic: To ‘Err’ is Human

    Link: http://www.siddharthrout.com/2011/08/01/to-err-is-human/

    4) What happens if there .CountA(Sheets("Sheet1").Cells) = 0? :) I would suggest you this code instead

        If WorksheetFunction.CountA(Sheets("Sheet1").Cells) > 0 Then
           LastCol = Cells.Find(What:="*", After:=[A1], _
           SearchOrder:=xlByColumns, _
           SearchDirection:=xlPrevious).Column
        Else
            MsgBox "No Data Found"
            Exit Sub
        End If
    

    5) ActiveSheet.Rows.Count will not give you the last active row. It will give you the total number of rows in that sheet. I would recommend getting the last row of Col A which has data.

    You can use this for that

    With Sheets("Sheet")
        LastRow =.Range("A" & .Rows.Count).End(xlup).row
    End With
    

    Now use LastRow instead of ActiveSheet.Rows.Count You also might want to use a For Loop so that you don't have to increment i every time. For example

    For i = 1 to LastRow
    

    6) Finally You should never use End. Reason is quite simple. It's like Switching your Computer using the POWER OFF button. The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Also the Object references held (if any) by other programs are invalidated.

    7) Based on your image in Chat, I believe you are trying to do this? This uses a code which doesn't use any loops.

    Option Explicit
    
    Sub FindFill()
        Dim wsI As Worksheet, wsO As Worksheet
        Dim DatesRange As Range
        Dim LastCol As Long, LastRow As Long
    
        If Application.WorksheetFunction.CountA(Sheets("Sheet1").Cells) = 0 Then
            MsgBox "No Data Found"
            Exit Sub
        End If
    
        Set wsI = Sheets("Sheet1")
        Set wsO = Sheets("Sheet2")
    
        With wsI
            LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            Set DatesRange = .Range("B1:" & Split(Cells(, LastCol).Address, "$")(1) & 1)
    
            .Columns(1).Copy wsO.Columns(1)
            DatesRange.Copy
            wsO.Range("B2").PasteSpecial xlPasteValues, _
            xlPasteSpecialOperationNone, False, True
    
            .Range("B2:" & Split(Cells(, LastCol).Address, "$")(1) & LastCol).Copy
            wsO.Range("C2").PasteSpecial xlPasteValues
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题