Code is not detecting the date.

H

Heera

Hi,

Here is my code.

The value in "C12" range is a date but my loop is not able to detect
the date in the column.


Sub RamPlan()

Workbooks("Ramp Plan Macro.xls").Activate
Sheets("Macro").Select

Dim fromdate As String

fromdate = Range("c12").Value

Windows(hirename).Activate
Sheets("C LLC").Select
Range("A2").Select

Do Until Selection.Value = "fromdate"
Selection.Offset(1, 0).Select
Loop
 
H

Heera

Then how should i declare?
I am new to the world of Macro and I am not even a programmer.

My problem.

I have two workbooks named "ramp plan macro" and "Hiring Plan"

This report depends upon the dates.

The macro should start working from the date which i have declared in
the "ramp pan macro" workbook.
 
A

Arvi Laanemets

Hi

I don't know, what format you have for Macro!C12 and for 'C LLC'!A:A, but
fromdate variable is a string, not a date. And whenever you compare a string
and date in VBA, they don't match.

Another remark - don't hop around the sheets of workbook - when more sheets
are involved, it makes user dizzy. And more importantly - moving cursor
around takes too much time.

So my advice is a code like:

Sub RamPlan()

Dim fromdate As Date, tabledate As Date
Dim lastrow As Long, i As Long
Dim found As Boolean

' the cell you are reading date from MUST be formatted as date, and the
value MUST be entered in valid date format
fromdate=Sheets("Macro").Range("C12")

tabledate=0
i=0
found=False
lastrow=Sheets("C LLC").UsedRange.Row()
For i=1 To lastrow-1
tabledate=Sheets("C LLC").Range("A" & 2+i)
If tabledate=fromdate
found=True
' what to do when date was found
Exit For
Else
' what to do when date wasn't found yet
End if
If found=False Then
' what to do when there wasn't such date in table at all
End If
Next i

End Sub
 
J

Joel

there are two problems

1) Do Until Selection.Value = "fromdate"
fromdate is a variable, remove the double quotes. Your code is looking for
the word "fromdate".

2) Dates in excel are not strings. Change the declaration to Variant.
Dim fromdate As Variant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top