简体   繁体   中英

excel vba method range of object _worksheet failed

can anyone help me fix my code? it says "method range of object _worksheet failed" but I can't find what's wrong with it.

I just mashed up a couple of codes to make this before and it worked nicely but now I can't find the error.

what this code does is open the file where it would want to copy the first sheet, put it in the workbook i'm using before the worksheet "Main Page", and it pastes everything to the bottom of the worksheet "BOM-DB" and then it deletes that worksheet and closes the workbook it got it from.

Private Sub CommandButton23_Click()
Application.DisplayAlerts = False
Dim wbk1 As Workbook, wbk2 As Workbook
Dim FileName As String, FileToOpen As String

FileToOpen = Application.GetOpenFilename _
(Title:="Choose Excel File to Import", _
FileFilter:="Excel Files *.xl?? (*.xl??),")
If FileToOpen = "False" Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Open(FileName:=FileToOpen)

wbk2.Sheets.Copy before:=Workbooks(ThisWorkbook.Name).Sheets("Main Page")

Dim ws1 As Worksheet, ws2 As Worksheet
Dim i As Integer, k As Integer
Dim ws1LR As Long, ws2LR As Long

Set ws1 = Sheets(1)
Set ws2 = Sheets("BOM-DB")

ws1LR = ws1.Range("AA" & Rows.Count).End(xlUp).Row + 1
ws2LR = ws2.Range("AA" & Rows.Count).End(xlUp).Row

i = 2
k = ws2LR
Do Until i = ws1LR
    With ws1
        .Range(.Cells(i, 1), .Cells(i, 27)).Copy
    End With

    With ws2
        .Cells(k, 1).Offset(1, 0).PasteSpecial
    End With

    k = k + 1
    i = i + 1
Loop
wbk2.Close
ThisWorkbook.Sheets(1).Delete
End If
Application.DisplayAlerts = True
End Sub

the excel file its always getting from ranges from A to AA.

I made some changes in your code as seen below:

Edit1: Explicitly declare After Argument

Dim wbk1 As Workbook, wbk2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim FileToOpen As String

FileToOpen = Application.GetOpenFilename _
    (Title:="Choose Excel File to Import", _
    FileFilter:="Excel Files *.xl?? (*.xl??),")
If FileToOpen = "False" Then
    MsgBox "No File Specified.", vbExclamation, "ERROR"
    Exit Sub
Else
    Set wbk1 = Thisworkbook: Set ws1 = wbk1.Sheets("BOM-DB")
    Set wbk2 = Workbooks.Open(FileName:=FileToOpen)
    DoEvents
    Set ws2 = wbk2.Sheets(1) '~~> I assume you have only 1 sheet?

    With ws2
        .Range("A2", .Range("AA" & .Rows.Count).End(xlUp)).Copy
        ws1.Range("A" & ws1.Range("A:A").Find("*", ws1.Range("A1") _
            , , , , xlPrevious).Row).Offset(1, 0).PasteSpecial xlPasteValues
    End With
End If
wbk2.Close False

Is this what you're trying. HTH.

Not sure about the error, but if I'm not mistaken, the PasteSpecial method without any additional arguments is essentially just pasting values only . If that is acceptable, then this will save you a few lines of code and should hopefully avoid the error:

Instead of:

With ws1
    .Range(.Cells(i, 1), .Cells(i, 27)).Copy
End With

With ws2
    .Cells(k, 1).Offset(1, 0).PasteSpecial
End With

Do this:

With ws1
    ws2.Cells(k+1, 1).Resize(1,27).Value = .Range(.Cells(i, 1), .Cells(i, 27)).Value
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM