Highest & lowest value for multiple columbs

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
need to create a VBA code that gets the difference between the highest and lowest value for each name in a new tab (lets call this new sheet for the output “output or difference”

Input:
NameappleTrees
A12
A26
A47
A62
B35
C13
C36
C67

nameAppleTrees
A55
B35
C31
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Book1
ABCDEFG
1NameappleTreesNameDiff ApplesDiff Trees
2A12A55
3A26B00
4A47C54
5A62
6B35
7C13
8C36
9C67
Sheet1


Power Query Mcode

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Name"}, {{"Max Apple", each List.Max([apple]), type number}, {"Min Apple", each List.Min([apple]), type number}, {"Max Trees", each List.Max([Trees]), type number}, {"Min Trees", each List.Min([Trees]), type number}}),
    #"Inserted Subtraction" = Table.AddColumn(#"Grouped Rows", "Subtraction", each [Max Apple] - [Min Apple], type number),
    #"Renamed Columns" = Table.RenameColumns(#"Inserted Subtraction",{{"Subtraction", "Diff Apples"}}),
    #"Inserted Subtraction1" = Table.AddColumn(#"Renamed Columns", "Subtraction", each [Max Trees] - [Min Trees], type number),
    #"Renamed Columns1" = Table.RenameColumns(#"Inserted Subtraction1",{{"Subtraction", "Diff Trees"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns1",{"Max Apple", "Min Apple", "Max Trees", "Min Trees"})
in
    #"Removed Columns"
 
Upvote 0
Can you explain how you arrive at your results for values B and C?
If I subtract the Lowest value fromt he Highest Value, I get the results shown by alansidman, not yours.

For value B, since there is only one record, it would seem that the Highest and Lowest values are exactly the same, so anything minus itself is 0.

And column value C, we have:

Apple: Highest (6) - Lowest (1) = 5
So I am not sure how you arrive at 3.

Likewise, we have:
Trees: Highest (7) - Lowest (3) = 4
So I am not sure how you arrive at 1.

And are you still really using Excel 2013?
I think this may be much easier in the newer versions of Excel that have the MAXIFS and MINIFS functions.
 
Upvote 0
Can you explain how you arrive at your results for values B and C?
If I subtract the Lowest value fromt he Highest Value, I get the results shown by alansidman, not yours.

For value B, since there is only one record, it would seem that the Highest and Lowest values are exactly the same, so anything minus itself is 0.

And column value C, we have:

Apple: Highest (6) - Lowest (1) = 5
So I am not sure how you arrive at 3.

Likewise, we have:
Trees: Highest (7) - Lowest (3) = 4
So I am not sure how you arrive at 1.
If the value only has one row, it doesn’t substract from anything. Keep the value as it is. As seen in Name=B.

Oops sorry. Yes my calculation is off. Here is my output
Nameapple Trees
A55
B35
C54
 
Upvote 0
OK, assuming that you have two sheets:
Input: this is where your original data currently resides
Output: this is where you want the output to go
and you data starts in cell A1, so your Input sheet looks like this:
1684267241020.png


You can use this VBA code:
VBA Code:
Sub MySummary()

    Dim wsIn As Worksheet
    Dim wsOut As Worksheet
    Dim lr As Long
    Dim lc As Long
    
    Application.ScreenUpdating = False
    
'   Designate Input and Output worksheets
    Set wsIn = Sheets("Input")
    Set wsOut = Sheets("Output")
    
'   Copy header row from Input Sheet to Output sheet
    wsIn.Range("1:1").Copy wsOut.Range("A1")
    
'   Copy first column from Input Sheet to Output sheet
    wsIn.Range("A:A").Copy wsOut.Range("A1")
    
'   Filter duplicates out of column A
    wsOut.Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
    
'   Find last row and column on Output sheet
    lr = wsOut.Cells(wsOut.Rows.Count, "A").End(xlUp).Row
    lc = wsOut.Cells(1, wsOut.Columns.Count).End(xlToLeft).Column
    
'   Populate formulas
    wsOut.Range(wsOut.Cells(2, "B"), wsOut.Cells(lr, lc)).Formula2R1C1 = _
        "=MAX(IF(Input!C1=Output!RC1,Input!C))-IF(COUNTIF(Input!C1,Output!RC1)>1,MIN(IF(Input!C1=Output!RC1,Input!C)),0)"
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro Complete!", vbOKOnly
    
End Sub
which should result in this on the Output sheet:
1684267339632.png
 
Upvote 0
OK, assuming that you have two sheets:
Input: this is where your original data currently resides
Output: this is where you want the output to go
and you data starts in cell A1, so your Input sheet looks like this:
View attachment 91725

You can use this VBA code:
VBA Code:
Sub MySummary()

    Dim wsIn As Worksheet
    Dim wsOut As Worksheet
    Dim lr As Long
    Dim lc As Long
   
    Application.ScreenUpdating = False
   
'   Designate Input and Output worksheets
    Set wsIn = Sheets("Input")
    Set wsOut = Sheets("Output")
   
'   Copy header row from Input Sheet to Output sheet
    wsIn.Range("1:1").Copy wsOut.Range("A1")
   
'   Copy first column from Input Sheet to Output sheet
    wsIn.Range("A:A").Copy wsOut.Range("A1")
   
'   Filter duplicates out of column A
    wsOut.Columns("A:A").RemoveDuplicates Columns:=1, Header:=xlYes
   
'   Find last row and column on Output sheet
    lr = wsOut.Cells(wsOut.Rows.Count, "A").End(xlUp).Row
    lc = wsOut.Cells(1, wsOut.Columns.Count).End(xlToLeft).Column
   
'   Populate formulas
    wsOut.Range(wsOut.Cells(2, "B"), wsOut.Cells(lr, lc)).Formula2R1C1 = _
        "=MAX(IF(Input!C1=Output!RC1,Input!C))-IF(COUNTIF(Input!C1,Output!RC1)>1,MIN(IF(Input!C1=Output!RC1,Input!C)),0)"
   
    Application.ScreenUpdating = True
   
    MsgBox "Macro Complete!", vbOKOnly
   
End Sub
which should result in this on the Output sheet:
View attachment 91727
Hi yes this works! but
1. can you make it so that in case Name doesn't exist in the first column, it would still work. For example, Name exists in Column D or F.
2. Also are you able to change the columns i calculate for? Sometimes Apple and Trees columns won't be right after one another. Locating this column
 
Upvote 0
Hi yes this works! but
1. can you make it so that in case Name doesn't exist in the first column, it would still work. For example, Name exists in Column D or F.
2. Also are you able to change the columns i calculate for? Sometimes Apple and Trees columns won't be right after one another. Locating this column
If you want code more tailored to your exact structure, then I need you to provide exact details instead of general ones.
I need to know exact ranges of where things exist.
 
Upvote 0

Forum statistics

Threads
1,215,579
Messages
6,125,646
Members
449,245
Latest member
PatrickL

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