Results 1 to 14 of 14

Sorting/counting/summing by step Macro help.

Threaded View

  1. #1
    Registered User
    Join Date
    01-25-2012
    Location
    Trenton, NJ
    MS-Off Ver
    Excel 2010
    Posts
    7

    Sorting/counting/summing by step Macro help.

    Hello Excel Forum Experts!

    This is my first post in what I hope will become an illustrious forum career. I regularly need to sort a data set by steps. The steps will be in pound increments with a corresponding, proportional number of units. The steps go from 1-10 and then in increments of 5 above 10. I've already created the code to create the steps and the corresponding increment numbers. The next step is the sorting. As you'll see in the attached image, I'd like to input the number of steps, the increment size, and the data list and have the macro create the table and count and sum the number of units in each step. For example, if there were three steps and the increment was 100, the steps would be 100, 200, and 300. If the data was 100,101,200, and 300, the steps would be 1-(1count, 100sum), 2-(2count, 201sum), 3-(1count, 300 sum). I'm thinking there will need to be another loop embedded in the one I have already, this one will populate the sum and count columns. Here is the code I've got so far:

    Sub StepPopulator()
    '
    ' StepPopulator Macro
    ' Takes value of highlighted cell and value of cell to the right and creates a step table based on number of steps, and incrememnt size.
    '
    
        Dim Steps As Integer
        Dim Step As Integer
        Dim Increments As Integer
        Dim Increment As Integer
        Dim CountShips As Integer
        Dim SumUnits As Long
        Dim Data As Ranges
        Range("B5").Select
        Steps = ActiveCell
        Increments = ActiveCell.Offset(1, 0)
        
    ' The section below will place column headers in the table
        
        ActiveCell.Offset(0, 3) = "Pounds"
        ActiveCell.Offset(0, 4) = "Units"
        ActiveCell.Offset(0, 5) = "# of Shipments"
        ActiveCell.Offset(0, 6) = "Total Units"
        
        
    ' The section below will populate the Step and Increment table
    
            For X = 1 To Steps
                Step = X
                If X > 10 Then Step = (((X - 10) * 5) + 10)
                Increment = Step * Increments
                ActiveCell.Offset(X, 3) = Step
                ActiveCell.Offset(X, 4) = Increment
                ActiveCell.Offset(X, 5) = CountShips
                ActiveCell.Offset(X, 6) = SumUnits
            Next X
    ' The section below will total the # of shipments and the total units
        
        ActiveCell.Offset((Steps + 1), 4) = "Total"
        ActiveCell.Offset((Steps + 1), 5) = "Count"
        ActiveCell.Offset((Steps + 1), 6) = "Total Units"
        
    End Sub
    Attachment 138058

    **Update - Attached worksheet. The steps and increments in the output table were populated by the macro, the sums and counts were manually calculated. I'm looking for an efficient way to count and sum based on step. I'd created a huge string of if's to do this, but creating it manually was cumbersome, and I'm sure there is a better way.Attachment 138072Attachment 138073
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by TCDataAnalyst; 01-25-2012 at 04:55 PM. Reason: Done some more work on code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1