Weekday function

Valentino

Board Regular
Joined
Mar 28, 2010
Messages
105
Hi All,

In one of my macro's, which performs submacro's for weekdays only, I would like to add a certain macro which only performs if the weekday is a friday. However, as i've set it up now, it completely skips the submacro even on fridays. Can somebody help me on this, as I'm not sure what I'm overlooking? Thx

The original code of the main macro is:


Sub RunPeriod()

Dim i As Date

i = Range("StartTest")

Do While i < Range("LastMarket") + 1

If Weekday(i, vbSaturday) > 2 Then

Range("b2").Value = i
[Macro]
Debug.Print i
End If

i = i + 1


Loop

End Sub


The new submacro, which is part of "Macro" in the code above, is of the form:

Sub NewWeek()
'
' NewWeek Macro
'

'
If Weekday(i, vbSaturday) > 6 Then
[Submacro code]
End If

As indicated, it moves immediately to "End if", even on Fridays. What am I doing wrong??

Hopefully I've described the problem clearly, if not please indicate as I will be happy to give additional info to get this gremlin solved!

Thanks in advance

Valentino
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
If I understand you correctly, you've declared i in RunPeriod so it only has a value in (i.e. it's local to) that procedure. When NewWeek executes, i does not exist and therefore has no value. If you place the statement MsgBox i before the End If in NewWeek, you can check whether this is true.

Please do that first.

If that is the case, to fix it - assuming RunPeriod and NewWeek are in the same code module - move the DIM statement for i to the top of the module so it becomes global to the entire module (and all the procedures in it) rather than local to RunPeriod.

In future, I recommend you head all your code modules with the Option Explicit directive: this would have highlighted the error immediately.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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