我希望执行以下操作:
到目前为止,我有这个:
Private Sub CommandButton1_Click()
Dim SourceFile As String
Dim SourceBook As Workbook
Dim DestinationBook As Workbook
Dim desiredName As String
Set DestinationBook = ThisWorkbook
SourceFile = Application.GetOpenFilename(fileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
Set SourceBook = Workbooks.Open(SourceFile)
SourceBook.Sheets("Performance").Activate
desiredName = ActiveSheet.Range("I2")
Application.CutCopyMode = True
SourceBook.ActiveSheet.Range("E25:I64").Copy
DestinationBook.Activate
If WorksheetExists = False Then
MsgBox "Couldn't find " & desiredName & " sheet within destination workbook"
Call SourceBook.Close(False)
Exit Sub
Else
Range("E25:I64").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Call SourceBook.Close(False)
End If
End Sub
Function WorksheetExists() As Boolean
Dim sh As Object
For Each sh In DestinationBook.Worksheets
If sh.Name = desiredName Then WorksheetExists = True: sh.Activate
Exit For
Next
End Function
我收到运行时错误' 424':需要对象
有任何建议......?
提前致谢!
答案 0 :(得分:1)
以下是对您最新代码的修改。请注意这些添加:1)“Option Explicit”确保您已正确声明所有变量,2)变量已分配给重要的工作簿,工作表和范围,3)所需的变量传递给WorkSheetExists
函数。要使其工作,DestinationBook
中应该有名为“Performance”和“testSheet”的工作表,I2
的{{1}}中应该有“testSheet”。请记住,这只是“让你前进”的尝试,所以我希望你需要修改。
SourceBook