运行时错误1004,应用程序定义或对象定义的错误
我到处search,看看为什么我得到这个错误。 基本上,一旦我到达最后一行“Selection.AutoFill Destination:= Range(”G2:M“&LR)”我得到错误。 代码在单独的子版本中起作用。 因此,我假设上面的代码是以某种方式影响它?
Sub Certainsheets() Dim Wb1 As Workbook, wb2 As Workbook, wb3 As Workbook Dim LR As Long Dim rTable As Range Dim strCellREF2Txt As String Dim strFILEname As String Dim WS As Worksheet 'copy from ThisWorkbook 'Set wb2 = Workbooks(2) Set wb2 = Workbooks.Open("C:\Users\asharma\Desktop\Loan Application\Loan Data.xls") 'To this Set Wb1 = ThisWorkbook 'Copying data from Loan Data file Set tbl = wb2.Sheets(1).Range("A1").CurrentRegion tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Copy 'wb2.Sheets(1).Range("A1").CurrentRegion.Copy 'Pasting data into AOL DATA Tab Wb1.Activate Sheets("AOL DATA").Range("A10000").End(xlUp).Offset(1, 0).PasteSpecial xlValues 'Wb1.Sheets(1).Range("A1").Select.PasteSpecial Paste:=xlPasteValues, 'Operation:=xlNone, SkipBlanks _ ':=False, Transpose:=False Application.CutCopyMode = False wb2.Close 'REMOVING DUPLICATES 'Sheets("AOL DATA").Range("$A:$E").RemoveDuplicates Columns:=1, Header:=xlNo 'This part Autofills the formulas till the last row. LR = Range("A" & Rows.Count).End(xlUp).Row Sheets("AOL DATA").Range("G2:M2").Select Application.CutCopyMode = False Selection.AutoFill Destination:=Range("G2:M" & LR) End sub'
任何帮助,将不胜感激
您需要使用实际工作表限定您的范围,否则VBA将默认为ActiveSheet
对象,这可能不是您在代码中所期望的。
您可以重新编写代码,如下所示:
Sub Certainsheets() Dim loanWorkbook As Excel.Workbook Dim aolSheet As Excel.Worksheet Dim dataTable As Excel.Range Set loanWorkbook = Workbooks.Open("C:\Users\asharma\Desktop\Loan Application\Loan Data.xls") Set aolSheet = ThisWorkbook.Sheets("AOL DATA") Set dataTable = loanWorkbook.Sheets(1).Range("A1").CurrentRegion With dataTable.Offset(1, 0) aolData.Range("A" & aolData.Rows.Count).End(xlUp).Offset(1, 0).Value = _ .Resize(.Rows.Count - 1, .Columns.Count).Value End With loanWorkbook.Close With aolSheet .Range("G2:M2").AutoFill .Range("G2:M" & .Cells(.Rows.Count, 1).End(xlUp).Row) End With End Sub
AutoFill()
方法要求将源范围作为目标范围的一部分包含在内。 我怀疑是因为你的代码依赖于ActiveSheet
对象,你不知不觉地在不同的工作表上指定了两个范围,所以代码失败了。