String to date object? - go back a custom number of days? :)

apwdweb

Board Regular
Joined
Aug 23, 2002
Messages
86
Hi All,

I'm stumped and wonder if anyone can help me figure this one out

I have a custom date stored via three integer variables a,b,c

a = 10 '10 Dec
b = 12 '12th
c = 2003 '2003 year

Suppose i need to convert this date to a date that is exactly 30 days back or 90 days or better yet a Custom number of days back.

I was wondering if there is a good way to do this accurately to reflect leap years and the number of days for each month.

Would appreciate your input.
Thanks in advance
Jennifer
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Using VBA or macro for date less 30 days

a = 10
b = 12
c = 2003
MsgBox DateSerial(c, b, a) - 30

using formula
=date(c,b,a)-30
 
Upvote 0
Re: String to date object? - go back a custom number of days

heres something to try
just an example I made up

hope it helps
steve w


Private Sub CommandButton1_Click()
Dim a As Variant
Dim b As Variant
Dim c As Variant


a = 10 '10 Dec
b = 12 '12th
c = 2003 '2003 year

TextBox1.Value = DateSerial(c, b, a)

TextBox2.Value = DateSerial(c, b, a) - TextBox3.Value 'textbox3 has value you want to subtract

End Sub
 
Upvote 0
Re: String to date object? - go back a custom number of days

Great! thank you!

now what if i want to convert the date back to a string after i subtract (go back) 90 days?

Thanks
 
Upvote 0
a = 10
b = 12
c = 2003
ActiveCell = "'" & DateSerial(c, b, a) - 90
a = Day(DateSerial(c, b, a) - 90)
b = Month(DateSerial(c, b, a) - 90)
c = Year(DateSerial(c, b, a) - 90)
 
Upvote 0
Re: String to date object? - go back a custom number of days

chitosunday

Thanks for your help. DO you know if it is possible to count 90 days backwards excluding weekends?

Thanks.
 
Upvote 0
'use the workday function
a = 15
b = 1
c = 2004
ActiveCell.Formula = "=workday(" & """" & DateSerial(c, b, a) & """" & ",-90)"
ActiveCell.Value = "'" & ActiveCell.Value
a = Day(ActiveCell)
b = Month(ActiveCell)
c = Year(ActiveCell)
 
Upvote 0
or use this

a = 15
b = 1
c = 2004
ActiveCell = Format(Application.ExecuteExcel4Macro("workday(""" & DateSerial(c, b, a) & """,-90)"), "mm-dd-yy")
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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