Trying to figure out VBA Solution for text date minus 1

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,177
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If your input is 1232019 what do you want the output to be 1222019 or 1222019?

To put it another way, does 1232019 represent 23. Jan 2019 or 3. Dec 2019 ?
 
Last edited:
Upvote 0
Hi mikerickson the leading 0's are dropped.
The 12312018 represents December 31
The 1022019 represents 01022019
 
Upvote 0
If the day portion retains its leading 0, try this

=TEXT(DATE(RIGHT(A1,4),LEFT(RIGHT("0"&A1,8),2),MID(RIGHT("0"&A1,8),3,2))-1, "mddyyyy")
 
Upvote 0
What number represents 23 Jan 2019? 1232019
What number represents 3 Dec 2019? 12032019
 
Upvote 0
mikerickson thank you very much, that works a for a formula but what would the VBA approach be?
 
Last edited:
Upvote 0
Code:
Function NextDate(thisDate as String) As String
    Dim currentDate as Date
    thisDate = Right("0" & thisDate, 8)
    currentDate = DateValue(Right(thisDate,4), Left(thisDate,2), Mid(thisDate, 3, 2))
    NextDate = Format(currentDate - 1, "mddyyyy")
End Function
 
Upvote 0
Hi mikerickson,

I am getting a Compile Error: Wrong number of arguments or invalid property assignment on this line
currentDate = DateValue(Right(thisDate,4), Left(thisDate,2), Mid(thisDate, 3, 2))
 
Upvote 0
[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]
1012019​
[/td][td="bgcolor:#CCFFCC"]12312018[/td][td="bgcolor:#CCFFCC"]B2: =DayPrior(A2)[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
1022019​
[/td][td="bgcolor:#CCFFCC"]01012019[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]
4012018​
[/td][td="bgcolor:#CCFFCC"]03312018[/td][td][/td][/tr]
[/table]


Code:
Function DayPrior(thisDate As String) As String
    DayPrior = Format(DateValue(Format(thisDate, "00\/00\/0000")) - 1, "mmddyyyy")
End Function
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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