Excel VBA循环仅适用于前两张图纸-如何修复?
我已经在这个宏上工作了几天了,遇到了一个用有限的VBA知识无法解决的问题。它在前两张工作表中工作正常,但在这之后,它似乎出现了问题,没有从第二个工作簿(宏海关发票)返回正确的范围,或者重新合并此工作簿中的单元格,因此最终pdf没有我想要的信息,因为格式完全错误。请帮助我,好人!非常感谢你的帮助Excel VBA循环仅适用于前两张图纸-如何修复?,excel,vba,Excel,Vba,我已经在这个宏上工作了几天了,遇到了一个用有限的VBA知识无法解决的问题。它在前两张工作表中工作正常,但在这之后,它似乎出现了问题,没有从第二个工作簿(宏海关发票)返回正确的范围,或者重新合并此工作簿中的单元格,因此最终pdf没有我想要的信息,因为格式完全错误。请帮助我,好人!非常感谢你的帮助 Sub VBAexperimentalv6() Dim invoice As Workbook Dim invoicews As Worksheet Dim origlotno As Range Di
Sub VBAexperimentalv6()
Dim invoice As Workbook
Dim invoicews As Worksheet
Dim origlotno 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
invoicews.Activate
Set origlotno = Range("D15", Range("D15").End(xlDown))
Set invoiceci = Range("F15", Range("F15").End(xlDown))
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.Columns.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
通常这里的问题是不合格的
Range
调用,例如在Set-origlotno=Range(“D15”,Range(“D15”).End(xlDown))
和Set-invoiceci=Range(“F15”,Range(“F15”).End(xlDown))
。您应该使用invoicews
来限定范围
呼叫,而不是以前激活工作表。谢谢您的回答!当我尝试此方法时,它不起作用,我得到1004“对象的方法范围\u工作表失败”消息。有没有其他方法可以鉴定这些单元格?这些列中的数据位于工作表的下方,但与我试图调用的范围不连续。这些行的编辑版本是什么?Set-origlotno=invoicews.range(“D15”,range(“D15”).End(xlDown))
Set-invoiceci=invoicews.range(“F15”,range(“F15”).End(xlDown))
您没有限定内部范围
调用<代码>范围(“D15”).End(xlDown)不合格。