IF statement in VBA code

JonnnyExcel

New Member
Joined
Feb 9, 2011
Messages
8
I am thinking an IF statement is what I will need but maybe not.

I have VBA code that finds the previous day's file and copies information into today's file. There are occasions when the previous day’s file doesn’t get generated, ie. no data, operator forgot to create file, etc. I need my macro to look for the previous day’s file and if it doesn’t find that file then look for the file from 2 days ago and so on.

I am already using the workday function so weekends and holidays are already figured in.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Thank you in advance.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I don't know what file naming convention you are using but here's an example of looking for a file name that has a date in the name e.g. "File ddmmyyyy.xls"

It finds the most recent dated file name.

Code:
    Dim MyFolder As String, MyFile As String
    Dim counter As Long

    MyFolder = "C:\Temp\"

    Do
        MyFile = Dir(MyFolder & "File " & Format(Date - counter, "ddmmyyyy") & ".xls")
        counter = counter + 1
    Loop Until MyFile <> "" Or counter = 100
    
    If MyFile <> "" Then
        MsgBox MyFile, vbInformation, "Most Recent File"
    Else
        MsgBox "No file found."
    End If
 
Upvote 0
Thank you for the response. Sorry for not replying sooner as I got side tracked on another task.

This works and I have revised the file naming to match my format.
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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