How do I subtract 2 days in VBA?

Status
Not open for further replies.

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Here is a copy and paste VBA code I currently use at work.

However, I am trying to add an IF and THEN statement because on Mondays I have to open and copy from a file with Saturdays date (where highlighted) instead of today's current date.

How would you recommend doing this?

Thanks.

Sub Grab_OBSB158_Data()

'

' Grab_DOB_Data Macro

'

' Keyboard Shortcut: Ctrl+h

'

Dim strYearShort As String, strMonthShort As String, strMonthLong As String, strDay As String, strYearLong As String

Dim strFullDate As String

Dim Drive As String

Dim strOpenPath As String

Dim strDestinationFileName As String

Dim OpenFile As String

Dim PasteFile As String



strYearShort = Format(Now, "yy")

strYearLong = Format(Now, "yyyy")

strMonthShort = Format(Now, "mm")

strMonthLong = Format(Now, "mmmm")

strDay = Format(Now, "DD")

strFullDate = strYearLong & strMonthShort & strDay

strMonthFolder = strMonthShort & "-" & strMonthLong

strFullDate2 = strMonthShort & "." & strDay & "." & strYearShort



'file location to open & grab

Drive = "X:"

strOpenPath = "\shareholder_accounting\529 C Share Restriction Controls\Fund Held\Delivered to Fund\EPM_output\"

strOpenPath = strOpenPath & strYearLong & "\" & strMonthFolder & "\"

OpenFile = "OBSB158_Delivered_to_Fund_" & strFullDate & ".xlsx"



strOpenPath = Drive & strOpenPath & OpenFile



'Name of file to paste

PasteFile = strFullDate2 & " Del to Fund Violations.xlsx"







'Open workbook:

Workbooks.Open strOpenPath



'grab Data And Copy

Sheets("Sheet1").Activate

Range("A:N").Select



Selection.Copy



'spot to paste data

Workbooks(PasteFile).Activate

Worksheets("Violations").Activate

Range("A1").Select



'paste data

ActiveSheet.Paste



End Sub
 

Attachments

  • image001.png
    image001.png
    49.8 KB · Views: 5

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Duplicate to: How do I subtract 2 days in VBA.

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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