Trying to figure out VBA Solution for text date minus 1

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,180
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have dates that are formatted a:
1012019
1022019
4012018

What I need to do is to take those dates and minus 1 from them:
1012019 = 12312018
1022019 = 1012019
4012018 = 3312018

I have a formula that I figured out below (It works but do not know if it the best solution)
Code:
=--TEXT(TEXT(A1,"00\/00\/0000")-1,"mmddyyyy")

What I am trying to do is to emulate this in VBA but I am stuck. I know in some of my other posts people say it is overkill to do it in VBA
but because I cannot do it in VBA, I need to learn and grow. Any help would be appreciated!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You're welcome.

Mike's function is fine also with DateValue changed to DateSerial.
 
Upvote 0
Here is one more way if could be done...
Code:
[table="width: 500"]
[tr]
	[td]Function DayPrior(thisDate As Long) As String
  DayPrior = Evaluate("TEXT(TEXT(" & thisDate & ",""00\/00\/0000"")-1,""mmddyyyy"")")
End Function[/td]
[/tr]
[/table]
which, of course, means this Excel formula solution would also work...

=TEXT(TEXT(A2,"00\/00\/0000")-1,"mmddyyyy")



Mike's function is fine also with DateValue changed to DateSerial.
And once that is done, Mike's function can be collapsed into a one-liner to match the other offerings here...
Code:
[table="width: 500"]
[tr]
	[td]Function NextDate(thisDate As String) As String
  NextDate = Format(DateSerial(Right(thisDate, 4), Left(Right("0" & thisDate, 8), 2), Left(Right(thisDate, 6), 2) - 1), "mmddyyyy")
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Thanks also Rick,
Not knowing much about evaluate, I have a question, I assume that Evaluate is used so that we do not have to use application.worksheetfunction for text i.e. (application.worksheetfunction.text)?
 
Upvote 0
Thanks also Rick,
Not knowing much about evaluate, I have a question, I assume that Evaluate is used so that we do not have to use application.worksheetfunction for text i.e. (application.worksheetfunction.text)?
Not exactly. While it could be used that way in some cases, the Evaluate function (with some need for coaxing in certain circumstances) gives the programmer the ability to calculate the result of certain types of Excel formulas. Note what I wrote as the argument to the Evaluate function and the formula I gave in that same message... do you see the similarity between them?
 
Upvote 0
Thanks Rick! Yes I do. Just watched a few videos and got a better understanding. Thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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