Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:51:08+00:00 2026-06-07T17:51:08+00:00

So, I had to make a macro that would run through a report and

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:51:10+00:00Added an answer on June 7, 2026 at 5:51 pm

    Near the end of the first/outer loop, we have these lines of code:

    Sheet1.Select
    Selection.Value = Selection.Offset(1, 0)
    InvoiceNum = Selection.Value
    ExtPrice = 0
    OrderFreight = 0
    If (Selection.Value = "") Then Exit Do
    

    The OP indicated in comments that removing this line worked:

    Selection.Value = Selection.Offset(1, 0)
    

    However, since Selection.Value is used on the very next line, I propose the following change to the next line as well (this was also suggested by @Scott in the comments).

    InvoiceNum = Selection.Offset(1, 0).Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had to make a really small and simple script that would fill a
I have had to make changes to a trigger and assumed that running an
I've had trouble writing this code. I'm supposed to make a function that can
If I had 1000 rows, would a single-line separator make the table view bigger
I had to make an Application that displays a form to search data from
I had to make a GUI which would allow my client to customise a
can i somehow find all functions/macros that take a specific type of parameter ?
Had to download Command Line Tools from Apple to get make to work, but
I had more database and I tried to make backup for them but they
i had soulation .i want to make navigation bar with items i will select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.