If Statement not working with Weekday function in VBA

joellen

New Member
Joined
Sep 7, 2018
Messages
4
This program works seamlessly when I have the cell referenced to today's date and it is Friday, but when I want to type in a date that isn't =Today() for some reason it does not recognize that the typed in date is a Friday.

Can someone help me figure out why that is? :confused: From my understanding, vbFriday is associated with an integer value (6) so when I say

Dim A6 As Integer

If Weekday(A6)=6 Then
Else

in theory, it should recognize this and save my file in the Completed Time Sheets folder I have pathed below if the typed in date is a Friday. But it keeps putting it in the Incompleted Time Sheets folder.

Thank you in advance!


Code:
Sub Workbook_BeforeClose()

Application.EnableEvents = False
Application.DisplayAlerts = False

A6 = Date


If Weekday(A6) = vbFriday Then
    ThisWorkbook.SaveAs ("C:\Time Sheets\Completed Time Sheets\" + "CMB(DONE)" + ".xlsm")
    
    ThisWorkbook.Saved = True
    Application.DisplayAlerts = True


Else


    ThisWorkbook.SaveAs ("C:\Time Sheets\Incompleted Time Sheets\" + "CMB(NOT DONE)" + ".xlsm")
    
    ThisWorkbook.Saved = True
    Application.DisplayAlerts = True
    
End If


  If Application.Workbooks.Count = 1 Then
        
        Application.Quit
        
    Else
    
        ActiveWorkbook.Close
        
    End If


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Bearing in mind that today is Wednesday, this works for me
Code:
Sub chk()
   Dim Dt As Date
   
   Dt = Date + 2
   If Weekday(Dt) = vbFriday Then
      MsgBox "Friday"
   Else
      MsgBox "Not friday"
   End If
End Sub
 
Upvote 0
This does work, but I'm trying to reference a later date, for example, if I type the date 01/04/2019 in the cell I'm referencing in that function, it saves my file in the Incomplete Time sheet Folder and does not recognize the 4th as being a Friday. Is this because the Weekday function can only reference today's date?
 
Upvote 0
You're referencing any cell in your code.
You have a variable called A6, should that be a cell reference?
 
Upvote 0
Should it not be Range("A6").Value?
Code:
If Weekday(Range("A6").Value) = vbFriday Then
 
Upvote 0
Boy do I feel silly.
This is exactly what I needed, I was trying to grab the cell as reference but the way I had it coded was setting a variable instead. Thank you for your help! :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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