所以,我必须创建一个宏,它将运行一个报告,并获取具有相同发票号的所有记录,并将它们与其日期,销售给谁,以及所有行的总和放在一行中。相同的发票编号的延长定价,订单运费(如果适用)以及延期定价和订单运费的总和。
它将采用这些值并将它们放入新的表格中。
这是我遇到问题的地方。在大多数情况下,似乎宏工作,但一旦达到某个记录,它实际上改变了数字的发票,即(123456)到下一行(即123457)因此,123456的发票不再现有的,而是与123457组合在一起。
我介入了它,并且应该没有理由让细胞改变价值,因为它没有为任何其他细胞做到这一点。有没有人有理由这样做呢?
这是我的代码。提前谢谢。
Sub CombineInvoice()
Dim InvoiceNum As String, InvoiceDate As String, SoldToAcct As String
Dim ExtPrice As Double, OrderFreight As Double, OrderTotal As Double
Dim WS2 As Worksheet
Application.ScreenUpdating = False
Set WS2 = Sheets.Add
With Sheet1.Range("A1,C1,D1,BB1,BD1")
.Copy Destination:=Sheet2.Range("A1:F1")
End With
Sheet1.Select
InvoiceNum = Range("A2").Value
InvoiceDate = Range("C2").Value
SoldToAcct = Range("D2").Value
Sheet2.Select
Selection.Offset(0, 5).Value = "Order Total"
Range("A2").Select
Sheet1.Select
Range("A2").Select
Do
'Set order freight
OrderFreight = OrderFreight + Selection.Offset(0, 55)
'Set invoice date
InvoiceDate = Selection.Offset(0, 2)
'Set Sold to account
SoldToAcct = Selection.Offset(0, 3)
'Get extended price
Do Until Selection.Value <> InvoiceNum
ExtPrice = ExtPrice + Selection.Offset(0, 53)
'Make sure orderFreight is the same
If (OrderFreight <> OrderFreight Or OrderFreight = 0) Then
OrderFreight = OrderFreight + Selection.Offset(0, 55)
Else
OrderFreight = OrderFreight
End If
Selection.Offset(1, 0).Select
Loop
'Add Extended Price to Order Freight
OrderTotal = ExtPrice + OrderFreight
'Populate Sheet 2 with data
Sheet2.Select
With Selection
.Value = InvoiceNum
.Offset(0, 1).Value = InvoiceDate
.Offset(0, 2).Value = SoldToAcct
.Offset(0, 3).Value = ExtPrice
.Offset(0, 4).Value = OrderFreight
.Offset(0, 5).Value = OrderTotal
.Offset(1, 0).Select
End With
'Return to sheet 1 for next invoice number
Sheet1.Select
Selection.Value = Selection.Offset(1, 0)
InvoiceNum = Selection.Value
ExtPrice = 0
OrderFreight = 0
If (Selection.Value = "") Then Exit Do
Loop
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
在第一个/外部循环的末尾附近,我们有以下几行代码:
Sheet1.Select
Selection.Value = Selection.Offset(1, 0)
InvoiceNum = Selection.Value
ExtPrice = 0
OrderFreight = 0
If (Selection.Value = "") Then Exit Do
OP在评论中指出删除此行有效:
Selection.Value = Selection.Offset(1, 0)
但是,由于Selection.Value
用于下一行,我建议对下一行进行以下更改(@Scott在评论中也提到了这一点。)
InvoiceNum = Selection.Offset(1, 0).Value