How to add days in date by macro

K

K

Hi all, I have TextBox1 on Sheet1 and in that TextBox1 I have date
like "15/02/2010". I made macro (see below) in which I tried to get a
date in cell A7 of Sheet2 which should be the date of 42 days ahead of
the date in TextBox1. My macro is not working and I am getting error
saying "Run-time error '13': Type mismatch". Please can any friend
help that what i am doing wrong.


Sub dd()
tx = Sheets("Sheet1").TextBox1.Value
With Sheets("Sheet2")
..Range("A7").Value = tx + 42
End With
End Sub
 
B

Bob Phillips

Try

Sub dd()
tx = Sheets("Sheet1").TextBox1.Text
With Sheets("Sheet2")
..Range("A7").Value = CDate(tx.Text) + 42
End With
End Sub

HTH

Bob
 
M

Mike H

Hi,

When you get your 'date' from the textbox it's a srtring and you have to
convert it to a date. Try this

Sub dd()
tx = Sheets("Sheet1").TextBox1.Value
Stop
With Sheets("Sheet2")
..Range("A7").Value = DateValue(tx) + 42
End With
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
M

Mike H

Hmm,

And of course you don't need the STOP command I left in by mistake!!

Sub dd()
tx = Sheets("Sheet1").TextBox1.Value
With Sheets("Sheet2")
..Range("A7").Value = DateValue(tx) + 42
End With
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.
 
K

K

Hi,

When you get your 'date' from the textbox it's a srtring and you have to
convert it to a date. Try this

Sub dd()
tx = Sheets("Sheet1").TextBox1.Value
Stop
With Sheets("Sheet2")
.Range("A7").Value = DateValue(tx) + 42
End With
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.






- Show quoted text -

Thanks guys. it works now
 

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