OR function within IF statement in VBA

Lee.

Board Regular
Joined
Dec 15, 2012
Messages
170
Hi,
I was wondering if someone can point me in the right direction with the following code please?

The background for the code is to loop through the next 180 days, and if the date is not a Saturday or Sunday then it will open the template and save it under the respective date. However, I get a mismatch error in the IF statement.

I can get it to work if I do
Code:
 IF zdate = "Monday" then 
....

etc.

But I want a 'smarter' code if possible

This is the code I've wrote which is not working

Code:
Dim zdate, savedate As String, i As Long
    For i = 1 To 180[INDENT]    zdate = Format(Date + i, "DDDD")[/INDENT]
[INDENT]    savedate = Format(Date + i, "DD.MM.YYYY") & ".xls"[/INDENT]
[INDENT=2]        If zdate Is Not "Saturday" Or "Sunday" Then[/INDENT]
[INDENT=2]        Workbooks.Open Filename:=ActiveWorkbook.Path & "\Template.xls"[/INDENT]
[INDENT=2]        ActiveWorkbook.SaveAs Filename:=savedate, _[/INDENT]
[INDENT=2]        accessMode:=xlShared[/INDENT]
[INDENT=2]        Windows(savedate).Close[/INDENT]
[INDENT=2]        Windows("MacroRunner.xls").Activate[/INDENT]
[INDENT=2]        End If[/INDENT]
    Next i

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Sorted

Changed first line to

Code:
                 If Not zdate = "Saturday" And Not zdate = "Sunday" Then
 
Last edited:
Upvote 0
Are you sure? Shouldn't it be

Code:
 If Not zdate = "Saturday" Or Not zdate = "Sunday" Then
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,560
Latest member
Torchwood72

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