Find max and min date in a column which needs to be found in row 1

Muktar888

New Member
Joined
Apr 30, 2017
Messages
17
Hi All,

Im am trying to use the find function to return the column letter in order to fetch the latest date in that column :

VBA Code:
Sub find_latest_date()

Dim max_dat As Date, min_dat As Date
Dim last_lin As Long
Dim ws As Worksheet
Dim rfind As Range
Set ws = ThisWorkbook.Worksheets("Paste")
Set ws2 = ThisWorkbook.Worksheets("Control")

last_lin = ws.Cells(Rows.Count, 1).End(xlUp).Row

With Range("A1:FF1")
 Set rfind = .Find(What:="ART start date", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
 If Not rfind Is Nothing Then
Routing_column = Routing_column = Split(rfind.Address, "$")(1)

End If
 End With


max_dat = Application.WorksheetFunction.Max(ws.Range("rfind" & "1" & "FF" & last_lin))


Call remove_nonblank_Last_ART_VL_Date

End Sub

Still an amateur here so any guidance would be useful, thanks!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You have some issue with this line here:
VBA Code:
max_dat = Application.WorksheetFunction.Max(ws.Range("rfind" & "1" & "FF" & last_lin))
Note that in VBA, everything enclosed in double-quotes is treated as literal text. So all variables should NOT be enclosed in double quotes.
So you need to refer to your rfind variable like this: rfind and not like this: "rfind"

However, I don't think that is what you want to use anyway. It looks like you want the column letter there, which is your Routing_column variable. rfind is a range.
Also, when creating the range address, if it is more than one cell, you need to separate the starting and ending cells with a semi-colon, like this: A1:J10.
So that range reference probab;y needs to look something like:
VBA Code:
Range(Routing_column & "1:FF" & last_lin)

Note. If you run into trouble building your range, it is always good to set it up as a variable, and then use a MsgBox to see if you are building the range string correctly, i.e.
VBA Code:
Dim rng as String
rng = Routing_column & "1:FF" & last_lin
MsgBox rng
max_dat = Application.WorksheetFunction.Max(ws.Range(rng))
 
Upvote 0
You have some issue with this line here:
VBA Code:
max_dat = Application.WorksheetFunction.Max(ws.Range("rfind" & "1" & "FF" & last_lin))
Note that in VBA, everything enclosed in double-quotes is treated as literal text. So all variables should NOT be enclosed in double quotes.
So you need to refer to your rfind variable like this: rfind and not like this: "rfind"

However, I don't think that is what you want to use anyway. It looks like you want the column letter there, which is your Routing_column variable. rfind is a range.
Also, when creating the range address, if it is more than one cell, you need to separate the starting and ending cells with a semi-colon, like this: A1:J10.
So that range reference probab;y needs to look something like:
VBA Code:
Range(Routing_column & "1:FF" & last_lin)

Note. If you run into trouble building your range, it is always good to set it up as a variable, and then use a MsgBox to see if you are building the range string correctly, i.e.
VBA Code:
Dim rng as String
rng = Routing_column & "1:FF" & last_lin
MsgBox rng
max_dat = Application.WorksheetFunction.Max(ws.Range(rng))
Thanks for the pointers! it helped me fix the issues and now works as expected!

Thanks @Joe4!!! Solved!
 
Upvote 0
You are welcome. Glad I could help!

:)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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