So, I had to make a macro that would run through a report and take all the records that had the same invoice number and put them into one row with its date, who it's sold to, the sum of all the rows of the same invoice number's extended pricing, the order freight if applicable, and the sum of the extended pricing and order freight.
It would take these values and put them into a new sheet.
Here is where I'm running in to a problem. For the most part, it seems like the macro works but once it gets to a certain record, it actually changed the invoice of the number i.e. (123456) to the next row's (i.e. 123457) Thus, the invoice that was 123456 is no longer existing and is instead grouped together with 123457.
I stepped through it, and there should have been no reason for the cell to change value, as it did not do it for any other cells. Does anyone have a reason as to why this is doing it?
Here's my code. Thanks in advance.
    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
 
    