1
因此,我必須創建一個宏,它可以運行一個報表並獲取具有相同發票號的所有記錄,並將其與日期放在一行中,它被賣給相同發票號碼的延長定價的所有行的總和,如果適用的話,定單運費以及延期定價和定單運費的總和。Microsoft Excel宏更改單元格的值當運行
它會把這些值,並把它們放到一個新的工作表。
這是我遇到問題的地方。大多數情況下,它似乎是宏觀的工作,但一旦它達到某個記錄,它實際上將數字的發票(即(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
我剛剛給了這些例子。但他們同樣是連續的。 – lessthanthree
兩件事。 1)使用選擇是VBA是一個100%的壞主意! (除非絕對必要,或者用於調試)。你指的是範圍或單元格要好得多。 2)如果沒有實際的數據和所有的選擇參考,很難進行測試,但是如果將「Selection.Value = Selection.Offset(1,0)| InvoiceNum = Selection.Value」行更改爲「InvoiceNum = Selection.Offset 1)「,這是否解決它。 –
啊。我想到了@阿蘭,我有一個不必要的結論。我註釋了SelectionValue = Selection.Offset(1,0),它運行得很好。不過謝謝你對我的問題的興趣! – lessthanthree