make cell active in beginning of macro

P

Pam

I am running the following code which works great for inserting a date into a
cell with a calendar, but it only works great if they are in the right active
cell when they click the macro. I need to reduce errors by making the active
cell b1 when they start the macro, do that it will check the date in that
cell, and return the date in that cell.
I'm lost., any help would be appreciated.
Thanks


Private Sub UserForm_Initialize()

If IsDate(ActiveCell.Value) Then
Calendar1.Value = DateValue(ActiveCell.Value)
Else
Calendar1.Value = Date

Range("B1").Value = Date

End If
End Sub
 
N

N10

Pam


Private Sub UserForm_Initialize()

Range("b1").select

If IsDate(ActiveCell.Value) Then
Calendar1.Value = DateValue(ActiveCell.Value)
Else
Calendar1.Value = Date

Range("B1").Value = Date

End If


Best N10

End Sub
 
J

Joel

Private Sub UserForm_Initialize()

If IsDate(Range("B1").Value) Then
Calendar1.Value = DateValue(Range("B1").Value)
Else
Calendar1.Value = Date

Range("B1").Value = Date

End If
End Sub
 
P

Pam

Thank you, I knew it was simple

N10 said:
Pam


Private Sub UserForm_Initialize()

Range("b1").select

If IsDate(ActiveCell.Value) Then
Calendar1.Value = DateValue(ActiveCell.Value)
Else
Calendar1.Value = Date

Range("B1").Value = Date

End If


Best N10

End Sub
 
P

Pam

Joel,
When I tried this it put the result in the cell I was in, although it seems
to be comparing the original cell?
 
J

Joel

Pam: The code you posted is only the initialize function when the userform is
opened. Check to see if the active cell gets changed before you enter
anything into the userform. If the active cell doesn't get changed during
the initialization then there is other code that is writing to the active
cell that you didn't post.

The code you posted should only change two items. Range "B1" and the
initial vbalue in Calandar1.value which I assume is some box on the form
(textbox, listbox, combobox).
 
P

Pam

Okay, the B1 is the cell that I want it to check to see if there's a date,
and change that if there is not a date, does that make sense?
If there is a date, I want it to leave it alone, if there is not, I want it
to default to today's date, I probably have the code wrong, but this will
work for now.
Thanks
 

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