使用GetOpenFilename“对象范围的默认方法失败”从另一个工作表导入数据
我试图创build一个button,从用户select的文件(上个月)导入数据到用户select的文件(本月)。
我定义了我的variables,并一切运作良好,直到我到达实际的导入位,在第一个工作簿的范围设置等于第二个工作簿的范围,我得到以下错误:
运行时错误“1004”
对象'Range'的方法'_Default'失败
两个文档都有相同的行数。 这里是适用的代码:
Option Explicit Public Sub ImportData() Dim InputFilename As Variant Dim OutputFilename As Variant Dim InputFile As Variant Dim InputSheet As Worksheet Dim OutputFile As Variant Dim OutputSheet As Worksheet 'Copy data from user-defined sheet to current open sheet 'User selects file to export data from InputFilename = Application.GetOpenFilename() Set InputFile = Workbooks.Open(InputFilename) Set InputSheet = InputFile.Sheets("Total Inventory") 'User defines file to import into OutputFilename = Application.GetOpenFilename() Set OutputFile = Workbooks.Open(OutputFilename) Set OutputSheet = OutputFile.Sheets("Total Inventory") Dim FirstRow As Integer Dim LastRow As Integer Dim UsedRange As Range Dim LastColumn As Range 'Find last Row of input file InputSheet.Activate FirstRow = 7 LastRow = ActiveSheet.UsedRange.Rows.Count 'Find last Column of input file Set LastColumn = InputSheet.Cells.Find(What:="*", _ after:=InputSheet.Cells(1), _ Searchorder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False)_ .Columns OutputSheet.Range(OutputSheet.Cells(FirstRow, 2), _ OutputSheet.Cells(LastRow, 2)).Value = _ InputSheet.Range(InputSheet.Cells(FirstRow, LastColumn), _ InputSheet.Cells(LastRow, LastColumn)).Value InputFile.Close End Sub
任何想法将不胜感激。
LastColumn
是Range
,导致Cells(FirstRow, LastColumn)
关于Cells
– > https://msdn.microsoft.com/en-us/library/ff196273(v=office.14).aspx
试试这个小小的修改:
'Find last Column of input file Dim LastColumnNum As Integer Set LastColumn = InputSheet.Cells.Find(What:="*", _ after:=InputSheet.Cells(1), _ Searchorder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False) LastColumnNum = LastColumn.Column OutputSheet.Range(OutputSheet.Cells(FirstRow, 2), _ OutputSheet.Cells(LastRow, 2)).Value = _ InputSheet.Range(InputSheet.Cells(FirstRow, LastColumnNum), _ InputSheet.Cells(LastRow, LastColumnNum)).Value
尝试这个 :
Option Explicit Public Sub ImportData() Dim InputFilename As String Dim OutputFilename As String Dim FirstRow As Integer Dim LastRow As Integer Dim LastColumn As Integer Dim InputFile As Workbook Dim OutputFile As Workbook Dim OutputSheet As Worksheet Dim InputSheet As Worksheet Dim UsedRange As Range 'Copy data from user-defined sheet to current open sheet 'User selects file to export data from InputFilename = Application.GetOpenFilename() Set InputFile = Workbooks.Open(InputFilename) Set InputSheet = InputFile.Sheets("Total Inventory") 'User defines file to import into OutputFilename = Application.GetOpenFilename() Set OutputFile = Workbooks.Open(OutputFilename) Set OutputSheet = OutputFile.Sheets("Total Inventory") 'Find last Row of input file FirstRow = 7 LastRow = InputSheet.Range("A" & Rows.Count).End(xlUp).Row 'Find last Column of input file LastColumn = InputSheet.Cells(1, Columns.Count).End(xlToLeft).Column 'Copy the data OutputSheet.Range(OutputSheet.Cells(FirstRow, 2), _ OutputSheet.Cells(LastRow, 2)).Value = _ InputSheet.Range(InputSheet.Cells(FirstRow, LastColumn), _ InputSheet.Cells(LastRow, LastColumn)).Value InputFile.Close End Sub
你正在input一个Range到Cells(),所以它可以工作