find max value using column name || userforms VBA

THE_NEW_XL_GUY

New Member
Joined
Dec 20, 2017
Messages
47
Hi guys,

Need some help in finding max value in a column with column name. like search max value in a column with the column name I provide.

conditions: column May or May not be present in sheet.
there are 3 or 4 columns with same column name and I need to check all.


current code I have just finds max value in range.

Code:
Private Sub Cmd_tt_today_bps_Click()
 
Dim fNameAndPath As Variant, wb As Workbook, ws As Worksheet

fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Set wb = Workbooks.Open(fNameAndPath)
max_num = Application.WorksheetFunction.Max(Range.Name) 
wb.Close savechanges:=False

Really need some directions on this. thanks in advance.
 
How about
Code:
   Dim Fnd As Range
   Dim Cnt As Long
   Dim Qty As Long
   Dim NewMax As Variant
   
   Set Fnd = Range("A1")
   Qty = WorksheetFunction.CountIf(Rows(1), "* in*")
   ReDim NewMax(1 To Qty)
   For Cnt = 1 To Qty
      Set Fnd = Rows(1).Find("* in*", Fnd, , xlWhole, , , False, , False)
      NewMax(Cnt) = Application.WorksheetFunction.Max(Fnd.EntireColumn)
   Next Cnt
   MsgBox Join(NewMax, ", ")
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
its working, its showing all 10 columns max values I.E 10 max values . what if I want THE max among those 10 values to be showed? and also help me to store the one max value among all max value in variable like the_max_value as I want to pass to a cell.
 
Upvote 0
That's exactly what the code I posted in post#3 should do.
 
Upvote 0
sorry if am dragging this, In post #3 code, where to provide max_num value?. we have not stored any values in max_num to compare with newmax right?


Dim Fnd As Range
Dim Cnt As Long
Dim Qty As Long
Dim NewMax As Variant

Set Fnd = Range("A1")
Qty = WorksheetFunction.CountIf(Rows(1), tb1.Value)
For Cnt = 1 To Qty
Set Fnd = Rows(1).Find(tb1.Value, Fnd, , xlWhole, , , False, , False)
NewMax = Application.WorksheetFunction.Max(Fnd.EntireColumn)
If NewMax > max_num Then max_num = NewMax
Next Cnt
 
Upvote 0
For the 1st col there will be no value in max_num so on this line
Code:
If NewMax > max_num Then max_num = NewMax
NewMax will be greater then max_num so we make max_num = NewMax.
For each subsequent col if NewMax is greater than max_num, we store that new number, otherwise it's left as is.
 
Upvote 0
okay.. Got it!!

here is the code I modified to my need.

Code:
Private Sub Cmd_max_Click()
 
   Dim fNameAndPath As Variant 
   Dim wb As Workbook 
   Dim ws As Worksheet
   Dim Fnd As Range
   Dim Cnt As Long
   Dim Qty As Long
   Dim newmax As Variant
   Dim dummy As Variant
   
fNameAndPath = Application.GetOpenFilename(fileFilter:="Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Set wb = Workbooks.Open(fNameAndPath)
Set Fnd = Range("A1")
 Qty = WorksheetFunction.CountIf(Rows(1), "* in*")
 For Cnt = 1 To Qty
 Set Fnd = Rows(1).Find("* in*", Fnd, , xlWhole, , , False, , False)
 newmax = Application.WorksheetFunction.Max(Fnd.EntireColumn)
 If newmax > dummy Then dummy = newmax
 
 Next Cnt
 
 nm = newmax ' I have declared nm outside of the sub and am assigning newmax value to nm, as value is used in another button.

and above code still shows max value of last column with the name "* in*"
 
Last edited:
Upvote 0
tried all the ways, still the code is finding max value in the last column. thanks a ton for helping me this for! Il find outa way! cheers!
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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