简体   繁体   English

对象_worksheet的excel vba方法范围失败

[英]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. 它说“对象_worksheet的方法范围失败”,但是我找不到它的问题所在。

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. 这段代码的作用是打开要复制第一张纸的文件,将其放在我正在使用的工作簿中,然后插入工作表“ Main Page”,然后将所有内容粘贴到工作表“ BOM-DB”的底部然后删除该工作表并关闭从中获取工作表的工作簿。

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. excel文件始终从A到AA。

I made some changes in your code as seen below: 我对您的代码进行了一些更改,如下所示:

Edit1: Explicitly declare After Argument Edit1:明确声明后争论

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. 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 . 不确定错误,但是如果我没记错的话,没有任何其他参数的PasteSpecial方法本质上只是粘贴值而已 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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