ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA find today's date in Column A (https://www.excelbanter.com/excel-programming/395994-vba-find-todays-date-column.html)

Bhupinder Rayat

VBA find today's date in Column A
 
Hi,

I have the date for everyday in column A from 2001 out to 2015. I want a
vba function that finds today's date in column A and makes it the active cell.

Can anyone help please?

Thanks,

B/

Gary''s Student

VBA find today's date in Column A
 
Sub find_date()
Dim d As Date, i As Long
d = Date
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Cells(i, "A").Value = d Then
Cells(i, "A").Select
Exit Sub
End If
Next
End Sub

--
Gary''s Student - gsnu200738


"Bhupinder Rayat" wrote:

Hi,

I have the date for everyday in column A from 2001 out to 2015. I want a
vba function that finds today's date in column A and makes it the active cell.

Can anyone help please?

Thanks,

B/


OssieMac

VBA find today's date in Column A
 
The following should allow you to enter a date at the input box and the find
method will find and activate the cell with the date. If not found then a
message is displayed.

Note the comment about editing to insert your worksheet name
Sub Find_Date()

Dim rng1 As Range
Dim dateStr As String
Dim dateToFind As Date
Dim foundDate As Range

'Get date as string value
dateStr = InputBox("Enter the date to be found")

'Convert string value to date format
dateToFind = DateValue(dateStr)

'Edit Sheet1 to your worksheet name
Set rng1 = Sheets("Sheet1").Range(Cells(1, 1), _
Cells(Rows.Count, 1).End(xlUp))

Set foundDate = rng1.Find(What:=dateToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundDate Is Nothing Then
foundDate.Select
Else
MsgBox dateStr & " not found"
End If


End Sub

Regards,

OssieMac

"Bhupinder Rayat" wrote:

Hi,

I have the date for everyday in column A from 2001 out to 2015. I want a
vba function that finds today's date in column A and makes it the active cell.

Can anyone help please?

Thanks,

B/


OssieMac

VBA find today's date in Column A
 
My apologies. You wanted to find today's date not a date you input and I
copied a macro that I had without amending it so try this code instead.

Sub Find_Date_2()

Dim rng1 As Range
Dim dateToFind As Date
Dim foundDate As Range

dateToFind = Date 'This is today's date

'Edit Sheet1 to your sheet name
Set rng1 = Sheets("Sheet1").Range(Cells(1, 1), _
Cells(Rows.Count, 1).End(xlUp))

Set foundDate = rng1.Find(What:=dateToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundDate Is Nothing Then
foundDate.Select
Else
MsgBox dateStr & " not found"
End If


End Sub

Regards,

OssieMac

"OssieMac" wrote:

The following should allow you to enter a date at the input box and the find
method will find and activate the cell with the date. If not found then a
message is displayed.

Note the comment about editing to insert your worksheet name
Sub Find_Date()

Dim rng1 As Range
Dim dateStr As String
Dim dateToFind As Date
Dim foundDate As Range

'Get date as string value
dateStr = InputBox("Enter the date to be found")

'Convert string value to date format
dateToFind = DateValue(dateStr)

'Edit Sheet1 to your worksheet name
Set rng1 = Sheets("Sheet1").Range(Cells(1, 1), _
Cells(Rows.Count, 1).End(xlUp))

Set foundDate = rng1.Find(What:=dateToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundDate Is Nothing Then
foundDate.Select
Else
MsgBox dateStr & " not found"
End If


End Sub

Regards,

OssieMac

"Bhupinder Rayat" wrote:

Hi,

I have the date for everyday in column A from 2001 out to 2015. I want a
vba function that finds today's date in column A and makes it the active cell.

Can anyone help please?

Thanks,

B/


Bhupinder Rayat

VBA find today's date in Column A
 
Both pieces of code are good.

Thanks Guys,

B/

"OssieMac" wrote:

My apologies. You wanted to find today's date not a date you input and I
copied a macro that I had without amending it so try this code instead.

Sub Find_Date_2()

Dim rng1 As Range
Dim dateToFind As Date
Dim foundDate As Range

dateToFind = Date 'This is today's date

'Edit Sheet1 to your sheet name
Set rng1 = Sheets("Sheet1").Range(Cells(1, 1), _
Cells(Rows.Count, 1).End(xlUp))

Set foundDate = rng1.Find(What:=dateToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundDate Is Nothing Then
foundDate.Select
Else
MsgBox dateStr & " not found"
End If


End Sub

Regards,

OssieMac

"OssieMac" wrote:

The following should allow you to enter a date at the input box and the find
method will find and activate the cell with the date. If not found then a
message is displayed.

Note the comment about editing to insert your worksheet name
Sub Find_Date()

Dim rng1 As Range
Dim dateStr As String
Dim dateToFind As Date
Dim foundDate As Range

'Get date as string value
dateStr = InputBox("Enter the date to be found")

'Convert string value to date format
dateToFind = DateValue(dateStr)

'Edit Sheet1 to your worksheet name
Set rng1 = Sheets("Sheet1").Range(Cells(1, 1), _
Cells(Rows.Count, 1).End(xlUp))

Set foundDate = rng1.Find(What:=dateToFind, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not foundDate Is Nothing Then
foundDate.Select
Else
MsgBox dateStr & " not found"
End If


End Sub

Regards,

OssieMac

"Bhupinder Rayat" wrote:

Hi,

I have the date for everyday in column A from 2001 out to 2015. I want a
vba function that finds today's date in column A and makes it the active cell.

Can anyone help please?

Thanks,

B/



All times are GMT +1. The time now is 09:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com