Converting Excel Formula to VBA Script

euge_prime2001

New Member
Joined
Aug 4, 2017
Messages
8
Hi Guys,
I would like to ask your help how to convert these excel formula into VBA Script:
Beg_Date: =DATE(YEAR(TODAY())-1,10,1)
End_Date: =TODAY()
Week No: = Beg_Date-End_Date

Thanks a lot for your help!

Regards,
Eugene
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I will give you the tools you need and let you do it.

The DATE function in Excel is equivalent to the DATESERIAL function in VBA.
The TODAY() function in Excel is equivalent to the DATE function in VBA.
the YEAR function is the same in both.
 
Last edited:
Upvote 0
Week No: = Beg_Date-End_Date
This will give you a negative number is this what you want?
 
Upvote 0
Sorry for that one.
Here's the correct excel formula
Beg_Date: =DATE(YEAR(TODAY())-1,10,1)
End_Date: =TODAY()
Week No: = End_Date-Beg_Date

Regards,
Eugene
 
Upvote 0
Sorry for that one.
Here's the correct excel formula
Beg_Date: =DATE(YEAR(TODAY())-1,10,1)
End_Date: =TODAY()
Week No: = End_Date-Beg_Date
Did you see my reply?
It is really just a simple substitution of using the function name equivalents I gave you.
 
Upvote 0
Hi Joe,
Thanks for your help.
Here's what i did to convert the said formula into VBA script.

mBegDate = DateSerial(Year(Now()) - 1, 10, 1)
mEndDate = Format(Now(), "yyyymmdd hhmm am/pm")
mWKNum = mEndDate - mBegDate

Regards,
Eugene
 
Upvote 0
Why did you apply the FORMAT function to your mEndDate calculation?
You did not do that in your original calculations. Don't do that here. The FORMAT function returns a Text value, not a Date value.
If you do that, you won't be able to use it in other calculations, like mWKNum.

There is no need to use the FORMAT function here. If it is being displayed anywhere on your sheet, just format the cell (not the value).
 
Upvote 0
You are welcome.

Note that the FORMAT function in VBA works the same way as the TEXT function does in Excel.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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