我收到运行时错误1004应用程序定义或对象定义的错误。 我在Excel中有一些VB,我试图让它选择一个范围(可能会改变)..它是错误的
Set SourceRange = WorkBk.Sheets("Sheet8").Range("B14", Range("XFD14").End(XltoLeft).Offset(0,2))
需要能够使SourceRange的范围B14(始终)到最后一个可用的单元格+ 2到右边(仅适用于情况)
我有:
Sub MergeData()
Dim SourceRange as Range
WorkBk As Workbook
Dim DestRange as Range
Dim FolderPath As String
Dim FileName As String
Dim NRow As Long
FolderPath = Sheets("Sheet1").Range("D7")
NRow = 4
FileName = DIR(FolderPath & "*.xlsb")
Do While FileName <> ""
Set WorkBk = Workbooks.Open(FolderPath & FileName)
WorkBk.Application.Run _
"'" & FileName & "'!auto_open"
Set SourceRange = WorkBk.Sheets("Sheet8").Range("B14", Range("XFD14").End(XltoLeft).Offset(0,2))
Set DestRange = Sheets("Sheet2").Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
DestRange.Value = SourceRange.Value
NRow = NRow + DestRange.Rows.Count
WorkBk.Close savechanges:=False
Loop
答案 0 :(得分:1)
不合格的Range("XFD14")...
是指活动工作表上的单元格,不一定是您预期的工作表。
更改为
With WorkBk.Sheets("Sheet8")
Set SourceRange = .Range("B14", .Range("XFD14").End(XltoLeft).Offset(0,2))
End With