How do I subtract 2 days in VBA.

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.
 

Attachments

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

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Sub Grab_OBSB158_Data()


' Grab_DOB_Data Macr


' Keyboard Shortcut: Ctrl+


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

Dim strFullDate As Stri

Dim Drive As Stri

Dim strOpenPath As Stri

Dim strDestinationFileName As Stri

Dim OpenFile As Stri

Dim PasteFile As Stri



strYearShort = Format(Now, "yy

strYearLong = Format(Now, "yyyy

strMonthShort = Format(Now, "mm

strMonthLong = Format(Now, "mmmm

strDay = Format(Now, "DD

strFullDate = strYearLong & strMonthShort & strD

strMonthFolder = strMonthShort & "-" & strMonthLo

strFullDate2 = strMonthShort & "." & strDay & "." & strYearSho



'file location to open & gr

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 & ".xls



strOpenPath = Drive & strOpenPath & OpenFi



'Name of file to pas

PasteFile = strFullDate2 & " Del to Fund Violations.xls






'Open workboo

Workbooks.Open strOpenPa


'grab Data And Cop

Sheets("Sheet1").Activa

Range("A:N").Sele



Selection.Co



'spot to paste da

Workbooks(PasteFile).Activa

Worksheets("Violations").Activa

Range("A1").Sele


'paste dat

ActiveSheet.Pas



End S
ub tea
ctteteta py cttey
thk:
x"te le x"\"\":"ab rtngay")")")")") ngngngngngngg
'h
'o
'lations").Activate

Range("A1").Select



'paste data

ActiveSheet.Paste



End Sub
 
Upvote 0
You must be desperate seen as you made 4 threads. Use this at the top of the procedure and then use d instead of now.

VBA Code:
Dim d As Date

If Weekday(Date) = vbMonday Then
    d = Date - 2
Else
    d = Date
End If
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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