Excel &引用;对象的方法范围“工作表失败”;何时从个人工作簿运行?
我刚刚完成了这个宏的工作,现在正在检查它是否适用于从数据库中获取的任何excel文档。虽然此maco在从工作簿中的运行时工作正常,但一旦转移到个人工作簿并在另一组数据上试用,我会得到“对象的方法范围\u工作表失败”-此代码行的运行时错误1004:Excel &引用;对象的方法范围“工作表失败”;何时从个人工作簿运行?,excel,vba,Excel,Vba,我刚刚完成了这个宏的工作,现在正在检查它是否适用于从数据库中获取的任何excel文档。虽然此maco在从工作簿中的运行时工作正常,但一旦转移到个人工作簿并在另一组数据上试用,我会得到“对象的方法范围\u工作表失败”-此代码行的运行时错误1004: Set rngC = invoicews.Range(rngB, invoicews.Range("D300")).Find("") 如果有人能告诉我如何解决这个问题,那就太好了,因为无论从哪个电子表格运行,
Set rngC = invoicews.Range(rngB, invoicews.Range("D300")).Find("")
如果有人能告诉我如何解决这个问题,那就太好了,因为无论从哪个电子表格运行,这个宏都是必不可少的。提前谢谢
Sub MakeCustomsInvoices()
Dim invoice As Workbook
Dim invoicews As Worksheet
Dim origlotno As Range
Dim rngA As Range
Dim rngB As Range
Dim rngC As Range
Dim rngD As Range
Dim invoiceci As Range
Dim macroci As Range
Dim remlotno As Range
Dim rhci As Range
Set invoice = ThisWorkbook
For Each invoicews In ThisWorkbook.Worksheets
Set rngA = invoicews.Range("D13:E16").Find("Lot", lookat:=xlPart) 'finds Lot header, goes down one cell
If Not rngA Is Nothing Then
Set rngB = rngA.Offset(1)
End If
Set rngC = invoicews.Range(rngB, invoicews.Range("D300")).Find("") 'finds first blank cell in lot no column, goes up one cell
If Not rngC Is Nothing Then
Set rngD = rngC.Offset(-1)
End If
Set origlotno = invoicews.Range(rngB, rngD) 'sets lot no range
Set invoiceci = rngB.Offset(0, 1)
Set invoiceci = invoiceci.Resize(origlotno.Rows.Count) 'sets location of customs info
Set remlotno = invoicews.Range("D15:E15")
Set remlotno = remlotno.Resize(origlotno.Rows.Count)
remlotno.UnMerge 'unmerges lot number column
With origlotno
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("A2").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2 'copies lot numbers from invoice to macro in dynamic ranges
End With
invoicews.Range("D5:K5").UnMerge 'unmerges client name cells
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("M3").Value = invoicews.Range("D5").Value ' copies client name to formula sheet
Set macroci = Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("J2")
Set macroci = macroci.Resize(invoiceci.Rows.Count)
invoiceci.Value = macroci.Value ' copies customs info into invoice
invoicews.Range("D5:K5").Merge 'merges client name
remlotno.Merge (True) ' merges lot no in variable range
invoicews.Rows("2:2").RowHeight = 60 ' sets address row to correct height
Set rhci = invoicews.Rows("15:15")
Set rhci = rhci.Resize(origlotno.Rows.Count)
rhci.RowHeight = 21 'resizes lot no rows to allow for two lines
Application.PrintCommunication = False
With invoicews.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
invoicews.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With invoicews.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.393700787401575)
.RightMargin = Application.InchesToPoints(0.196850393700787)
.TopMargin = Application.InchesToPoints(0.393700787401575)
.BottomMargin = Application.InchesToPoints(0.393700787401575)
.HeaderMargin = Application.InchesToPoints(0.393700787401575)
.FooterMargin = Application.InchesToPoints(0.393700787401575)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 0
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
invoicews.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Richard\Documents\Sortcoding\Customs Invoices\" & Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("M2").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False ' saves as pdf using formula generated file name
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("A2:A250").Clear ' clear formula sheet ready for next invoice
Next invoicews
End Sub
那是因为
rngB
什么都不是,我猜那是因为rngA
什么都不是。你有如果不是rngA什么都不是,那么但是其他部分在哪里?如果rngA
是无
,该怎么办?那会发生什么?如何设置rngB
。您假设rngA
不会是Nothing
。如果其中一张没有批次
,该怎么办?