Using Formulas Within VBA

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Trying to use the Max function within VBA to find the latest date within an array of data, but I don't want to result in a formula, but rather an actual number (date in this case).

Currently I put this together, but I'm trying to go with a method that doesn't require so much time for it to run and or output the result(s).

The idea is to find the latest date and put the result in R30.
VBA Code:
Sub test()

Sheets("Sheet2").Range("R30").Value = "=MAX(Sheet2!E32:E37)"
    With Sheets("Sheet2").Range("R30")
        .Value = Sheets("Sheet2").Range("R30").Value2
        .NumberFormat = "mm/dd/yy;@"
    End With

End Sub

I attempted to research the proper structure to pull this off, but probably wasn't using the correct phrasing. Any help or tips would be greatly appreciated!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This VBA code finds the max value in the sheet 2 E32 to E37 without using the worksheet, ( the real coders way!!)
VBA Code:
inarr = Worksheets("Sheet2").Range("E32:E37")
maxv = -2.2251E-308        ' start at smallest negative number
For i = 1 To UBound(inarr, 1)
  If inarr(i, 1) > maxv Then maxv = inarr(i, 1)
Next i
MsgBox maxv
 
Upvote 0
Would this give you the required result ?
VBA Code:
Sub test2()
    Sheets("Sheet2").Range("R30") = Format(WorksheetFunction.Max(Sheets("Sheet2").Range("E32:E37")), "mm/dd/yy;@")
End Sub

P.S. Hi to all.
 
Upvote 0
Solution
This VBA code finds the max value in the sheet 2 E32 to E37 without using the worksheet, ( the real coders way!!)
VBA Code:
inarr = Worksheets("Sheet2").Range("E32:E37")
maxv = -2.2251E-308        ' start at smallest negative number
For i = 1 To UBound(inarr, 1)
  If inarr(i, 1) > maxv Then maxv = inarr(i, 1)
Next i
MsgBox maxv
That's an approach I wasn't expecting, but I can definitely study this to learn from it.

Was attempting to seek something a little simpler, but I appreciate it!
 
Upvote 0
Hi

Another option:

VBA Code:
Sub test()
    
    With Worksheets("Sheet2").Range("R30")
        .NumberFormat = "mm/dd/yy;@"
        .Value = WorksheetFunction.Max(Worksheets("Sheet2").Range("E32:E37"))
    End With

End Sub
 
Upvote 0
Would this give you the required result ?
VBA Code:
Sub test2()
    Sheets("Sheet2").Range("R30") = Format(WorksheetFunction.Max(Sheets("Sheet2").Range("E32:E37")), "mm/dd/yy;@")
End Sub

P.S. Hi to all.
Works nicely and was the idea I had in mind, but the formatting doesn't apply.
 
Upvote 0
Hi

Another option:

VBA Code:
Sub test()
   
    With Worksheets("Sheet2").Range("R30")
        .NumberFormat = "mm/dd/yy;@"
        .Value = WorksheetFunction.Max(Worksheets("Sheet2").Range("E32:E37"))
    End With

End Sub
Nearly perfect, but struggling to fit this onto just 1 line if possible

Seeing how to apply the formula within VBA is helpful
 
Upvote 0
Format(WorksheetFunction.Max(Sheets("Sheet2").Range("E32:E37")), "mm/dd/yy;@")
Would this give you the required result ?
VBA Code:
Sub test2()
    Sheets("Sheet2").Range("R30") = Format(WorksheetFunction.Max(Sheets("Sheet2").Range("E32:E37")), "mm/dd/yy;@")
End Sub

P.S. Hi to all.
It has occurred to me that I make a mistake it appears.

This works perfectly. Thank you!

PS thanks everyone for the tips and methods.
 
Upvote 0
Thanks for the positive feedback(y), glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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