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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Please stop making duplicate threads.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,920
Messages
6,122,276
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