Alternative to if then

Adar123

Board Regular
Joined
Apr 1, 2018
Messages
83
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hi everyone,

I am using the following code to download data from a folder which is filled with files on a regular basis.

The files are supplied from Tuesday to Saturday, so on Monday I need to point not to DataX_CM_FUT1_" & dYes & "_" & dTod & "_000501UTC.csv" but to DataX_CM_FUT1_" & dFriday & "_" & dSaturday & "_000501UTC.csv"

I could create a parameter to say if today is Monday, then do this, otherwise do that. But this type of logic is a floodgate once there is more than two options to consider. Appreciate any 'smarter' solutions that will facilitate shorter code. Thank you.

VBA Code:
Sub copyColDataOld()

Dim lastRow As Long
Dim myApp As Excel.Application
Dim wkBk As Workbook
Dim wkSht As Object

Dim dTod As String, dYes As String

dTod = Format(Date, "yyyy-mm-dd")
dYes = Format(Date - 1, "yyyy-mm-dd")

Set myApp = CreateObject("Excel.Application")
Set wkBk = myApp.Workbooks.Open("Dump\DataX_CM_FUT1_" & dYes & "_" & dTod & "_000501UTC.csv")

lastRow = wkBk.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
wkBk.Sheets(1).Range("C2:H" & lastRow).Copy

myApp.DisplayAlerts = False
wkBk.Close
myApp.Quit
Set wkBk = Nothing
Set myApp = Nothing
Set wkBk = ActiveWorkbook
Set wkSht = wkBk.Sheets("NewData")
wkSht.Activate
Range("A1").Select
wkSht.Paste

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
.
What about a SELECT CASE statement ?
 
Upvote 0
Is this what you are proposing?

VBA Code:
    Select Case Format(Now(), "DDD") = "MON"
        Case vbMon
            Set wkBk = myApp.Workbooks.Open("Monday NAME")
        Case vbNonMon
           Set wkBk = myApp.Workbooks.Open("NonMonday NAME")
    End Select
 
Upvote 0
Format(Now(), "DDD") = "MON"
will actually return True or False.

So your code should look something like this:
VBA Code:
    Select Case Format(Now(), "DDD") = "Mon"
        Case True
            Set wkBk = myApp.Workbooks.Open("Monday NAME")
        Case False
            Set wkBk = myApp.Workbooks.Open("NonMonday NAME")
    End Select
Note that upper/lower case matters in this case, so you want to use "Mon" instead of "MON".

Of course, that could all be simplified to:
VBA Code:
If Format(Now(), "DDD") = "Mon" Then
    Set wkBk = myApp.Workbooks.Open("Monday NAME")
Else
    Set wkBk = myApp.Workbooks.Open("NonMonday NAME")
End If
 
Upvote 0
Thanks for pointing the Case sensitive issue. Both options work.
 
Upvote 0
You are welcome.
Glad it works for you!
:)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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