How to skip holiday while pulling data from past date.

Altamash

New Member
Joined
Jul 25, 2021
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
Need you help !


I am using below codes to copy data from another closed workbook to my macro workbook.
below is the code.Date in file name always changes other thing constant.


If i am running this report on Monday 16.08.21 and want data from date 12.08.21 thrusday.


Also if running report on tuesday and want data from 13.08.21 friday.
and if running report on wednesday and want data from Monday 16.08.21.


what should we do in this case.


also if i am running report and above dates are holiday and there is no file saved then how to skip.that date
please advise.



Sub FetchValidationPolicy()

Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Dim PreviousDate As String
Dim k As Integer

Set currentworkbook = ThisWorkbook

k = Weekday(Date, vbUseSystemDayOfWeek)

If k = 1 Then
PreviousDate = Format(Date - 3, "dd.mm.yyyy")
Else
PreviousDate = Format(Date - 2, "dd.mm.yyyy")
End If

Set sourceworkbook = Workbooks.Open("Q:\Financial_Operations\Finance Operations Team\Internal\45HC_General\HC.07 Refund Team\HC.07.08 Diary Review & Updates\Refund Update & Diary Review " & PreviousDate & ".xlsx")

sourceworkbook.Worksheets("Vadliation List").Range("B2:B500").Copy

currentworkbook.Activate
currentworkbook.Worksheets("Validation").Activate
currentworkbook.Worksheets("Validation").Cells(2, 1).Select
ActiveSheet.Paste

sourceworkbook.Close

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this

VBA Code:
Sub FetchValidationPolicy()

    Dim sourceworkbook As Workbook
    Dim currentworkbook As Workbook
    Dim PreviousDate As String
    Dim k As Integer
    Dim PrevDate As Date
    Const Holidays As String = "44381,44433,44445"  'numeric values of holidays, you will have to change these values
    Dim arr As Variant
    
    arr = Split(Holidays, ",")
     
    Set currentworkbook = ThisWorkbook
    
    k = Weekday(Date, vbUseSystemDayOfWeek)
    
    If k = 1 Then
        PreviousDate = Format(Date - 3, "dd.mm.yyyy")
        PrevDate = CDate(Date - 3)
    Else
        PreviousDate = Format(Date - 2, "dd.mm.yyyy")
        PrevDate = CDate(Date - 2)
    End If
    
    If WorksheetFunction.WorkDay(PrevDate - 1, 1, arr) = PrevDate Then
        Set sourceworkbook = Workbooks.Open("Q:\Financial_Operations\Finance Operations Team\Internal\45HC_General\HC.07 Refund Team\HC.07.08 Diary Review & Updates\Refund Update & Diary Review " & PreviousDate & ".xlsx")
        sourceworkbook.Worksheets("Vadliation List").Range("B2:B500").Copy
        currentworkbook.Activate
        currentworkbook.Worksheets("Validation").Activate
        currentworkbook.Worksheets("Validation").Cells(2, 1).Select
        ActiveSheet.Paste
        sourceworkbook.Close
    End If
End Sub
 
Upvote 0
Hi There,

Thanks, but i can't even run the codes.
Nothing is happening after running.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

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