VBA looping to remove min / max values and recompute average

BlueAZCoug

New Member
Joined
Dec 29, 2014
Messages
4
Hi everyone, I'm new to this site and hoping to get some help on VBA looping. I have a spreadsheet with financial data for several companies (example shown below, except that my real data is much larger, closer to 50 companies). I compute the average of the gross profit for all companies, and if 80% of the companies are within +/-15% of that average, then I'm done. If 80% of the companies are not within 15% of that average, I remove the maximum and minimum gross profit values and start the process over again by computing the average gross margin of the remaining companies (i.e. after taking out the min and max values). It's a very iterative/repetitive process, and with so many companies it's quite time consuming to do it by hand. I figured it would be easier to use VBA looping to whittle down the population until I get to my 80% target. Hopefully that explanation makes sense. I don't want to delete any of the information, so I'm not looking to delete any rows or anything with the code, I just want to exclude those companies from the calculation in some manner. I've never worked with looping in VBA (I'm somewhat of a VBA virgin if you will), but I'm willing to learn and hoping someone can point me in the right direction. Thanks for you time!


A​

B​

C​

D​

E​

F​

G​

H​

I​

J​

K​

1​

Iteration #1

Iteration #2

Iteration #3

2​

Company Name

2013 GM%

Within 15% of Avg?

Exclude?

Within 15% of Avg?

Exclude?

Within 15% of Avg?

Exclude?

3​
Company A
5.5%
No
Exclude
Exclude
Exclude

4​
Company B
21.1%
Yes
21.1%
Yes
Exclude
Exclude

5​
Company C
20.3%
Yes
20.3%
Yes
20.3%
Yes

6​
Company D
20.3%
Yes
20.3%
Yes
20.3%
Yes

7​
Company E
13.3%
No
13.3%
No
Exclude
Exclude

8​
Company F
42.8%
No
Exclude
Exclude
Exclude

9​

10​
# of companies within 15% of avg:
3
3
2

11​
Total # of companies:
6
4
2

12​
% of population within 15% of avg:
50%
75%
100%

13​
Result of test:
Fail
Fail
Pass

<tbody>
</tbody>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hello,

this may seem a dumb question, but what data, from your example, do you start with?
 
Upvote 0
onlyadrafter --

The data I'm starting with is in column C. In the first iteration I remove the highest and lowest values (shown in column E), in the second iteration I remove the next highest and lowest values (shown in column H), and so on. I would post the workbook but can't seem to figure out how to attach it to this thread. At any rate, there's got to be a better way to whittle down the data than going through this manual process, I just can't figure out how to do it with looping in VBA.
 
Upvote 0
Hello,

Have assumed that the cells in Column D in your example are formulas, so that when you copy the data over they get updated as per your posted example.

This code gives me the same result as per you example

Code:
Sub ITERATION()
    MY_START = 4
    MY_LOWEST = 100
    MY_HIGHEST = 0
'COPY OVER LAST ITERATION
    Do Until Cells(13, MY_START).Value = "Pass"
        Range(Cells(2, MY_START - 1), Cells(13, MY_START)).Copy
        Cells(2, MY_START + 2).PasteSpecial (xlPasteAll)
'REMOVE LOWEST AND HIGHEST VALUES
        For MY_COMPANY = 3 To 8
            If Cells(MY_COMPANY, MY_START + 2).Value < MY_LOWEST And _
                    Not IsEmpty(Cells(MY_COMPANY, MY_START + 2).Value) Then
                MY_LOWEST = Cells(MY_COMPANY, MY_START + 2).Value
                MY_LOW_CLEAR = MY_COMPANY
            End If
            If Cells(MY_COMPANY, MY_START + 2).Value > MY_HIGHEST And _
                    Not IsEmpty(Cells(MY_COMPANY, MY_START + 2).Value) Then
                MY_HIGHEST = Cells(MY_COMPANY, MY_START + 2).Value
                MY_HIGH_CLEAR = MY_COMPANY
            End If
        Next MY_COMPANY
        Cells(MY_LOW_CLEAR, MY_START + 2).ClearContents
        Cells(MY_HIGH_CLEAR, MY_START + 2).ClearContents
        MY_LOWEST = 100
        MY_HIGHEST = 0
        MY_START = MY_START + 3
    Loop
End Sub

Does it work as expected?
 
Upvote 0
onlyadrafter --

Yes, it works brilliantly, thank you for your help. However, I have a few questions about the code (just trying to understand the logic):

1) Obviously the data I posted above is just an example. If I have 100 companies (placed in rows 3 to 103), I just need to set "MY_COMPANY" = 3 To 103, correct?
2) How do the "MY_LOWEST" and "MY_HIGHEST" arguments work? I'm trying to understand why MY_LOWEST is set to 100 and not 0.

Thanks again, really do appreciate your help with this!
 
Upvote 0
Hello,

1) Yes, change 8 to the last row number of you company name.

2) MY_LOWEST starts at 100 because as these are %'s the highest value it could be is 100%. So the first number it looks at will be lower than 100, it will then compare the next number against this number and so on. MY_HIGHEST does the same but in opposite. I could have chosen numbers that are extremely high and extremely low (1E-255 and 1E+255).

OK?
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top