VBA; Select min or max in range depends on argument

KVikLine

New Member
Joined
Jan 30, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm truing to get min and max of range depends on selected argument via VBA. Unfortunately, without success.
An example of data source is attached.
E.g. I will found out (via VBA) which min and max "wi" contains to "th" = 7. Result should be: min =180 and max = 570.
Thank you for help.
thwi
10100
10250
10400
10550
10700
10850
9200
9330
9460
9590
9720
9850
9980
7180
7310
7440
7570
6203
6333
6463
6593
6723
6853
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the Board!

Why do you need VBA? This can be done pretty easily with the MINIFS and MAXIFS functions.
See: MINIFS Function
 
Upvote 0
Hi Joe4, I'm working on Add-In. This operation is a part of it.
 
Upvote 0
You can call/use Excel functions in VBA like this:
VBA Code:
Application.WorksheetFunction.MinIfs(...)
 
Upvote 0
Solution
Cool, thank you, it works....
Below extract based/from example:
thwith_sel
101006
10250
10400Answer_vbaAnswer (formula)
10550853max853
10700203min203
10850
9200
9330
9460
9590
9720
9850
9980
7180
7310
7440
7570
6203
6333
6463
6593
6723
6853


Formula:
Test_group.xlsm
F
4Answer (formula)
5853
6203
Sheet1
Cell Formulas
RangeFormula
F5F5=MAXIFS($B$2:$B$24,$A$2:$A$24,$D$2)
F6F6=MINIFS($B$2:$B$24,$A$2:$A$24,$D$2)


AND code in VBA (finally, will be adjusted to/in Add-In)

VBA Code:
Sub test_Th()
WBS_Name = ActiveWorkbook.Name
WSS_Name = ActiveSheet.Name
'choose th
th_sel = Workbooks(WBS_Name).Worksheets(WSS_Name).Cells(2, 4)
'verify min max based on selected th
Wi_max = Application.WorksheetFunction.MaxIfs(Workbooks(WBS_Name).Worksheets(WSS_Name).Range("B2:B24"), Workbooks(WBS_Name).Worksheets(WSS_Name).Range("A2:A24"), th_sel)
Wi_min = Application.WorksheetFunction.MinIfs(Workbooks(WBS_Name).Worksheets(WSS_Name).Range("B2:B24"), Workbooks(WBS_Name).Worksheets(WSS_Name).Range("A2:A24"), th_sel)
'write Answer
Workbooks(WBS_Name).Worksheets(WSS_Name).Cells(5, 4) = Wi_max
Workbooks(WBS_Name).Worksheets(WSS_Name).Cells(6, 4) = Wi_min
End Sub
 
Upvote 0
Excellent!

Glad I was able to help and it all worked out for you!

Note that if you want to shorten your formulas a little bit, you could set your ranges to range objects, and just use those in your formula (since you are using the same ranges for each formula).
Not necessary, but sometimes makes things look a little "cleaner".
 
Upvote 0
You mean like this?
Below an extract from Add-In:
VBA Code:
.........
Dim Wi_Range As Range
Dim Th_Range As Range
Set Wi_Range = Range(Workbooks(WB_Name).Sheets("Tabl_DimTab").Cells(2, 2), Workbooks(WB_Name).Sheets("Tabl_DimTab").Cells(Tabl_DimTab_LastRow, 2)) 
Set Th_Range = Range(Workbooks(WB_Name).Sheets("Tabl_DimTab").Cells(2, 1), Workbooks(WB_Name).Sheets("Tabl_DimTab").Cells(Tabl_DimTab_LastRow, 1)) 
'short and easy part like in row below?
Wi_Max = WorksheetFunction.Application.MaxIfs(Wi_Range, Th_Range, Th_to_use)
.........
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,265
Members
449,149
Latest member
mwdbActuary

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