Error 5 (Invalid procedure call or argument)

Stefan Braem

New Member
Joined
Jun 23, 2021
Messages
5
Hi

I'm trying to open another workbook based on the day of the week extracted from the date in the file name of the current workbook. This is my code:

Code:
Function OpenWerkboek()
    
    Dim Bestand As String
    Dim DatumDag As Long
    Dim DatumMaand As Long
    Dim DatumJaar As Long
    Dim Datum As Date
    Dim Dag As String
    Dim Pad As String
    Dim Werkboek As Workbook
    
    Bestand = ThisWorkbook.FullName

    DatumDag = CLng(Mid(Bestand, Len(Bestand) - 6, -2))
    DatumMaand = CLng(Mid(Bestand, Len(Bestand) - 8, -2))
    DatumJaar = CLng(Mid(Bestand, Len(Bestand) - 12, -4))
    
    Datum = DateSerial(DatumJaar, DatumMaand, DatumDag)
    Dag = Weekday(Datum)
    Pad = "C:\Users\stefa\Downloads\LOGBOEK 2021 (vanaf Juli)\03 WERKROOSTERS\" & Dag & ".xlsm"
    Werkboek = Workbooks.Open(Pad)

End Function

When I let it run, I get an Error 5 (Invalid procedure call of argument) but I can't seem to find out why. The error appears at the line indicated in red. Could someone help me on this?

Thanks in advance!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi & welcome to MrExcel.
You cannot use a negative number as the last argument in a Mid function.

You also need to use
VBA Code:
Set Werkboek = Workbooks.Open(Pad)
 
Upvote 0
Solution
For future reference.

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Error 5 (Invalid procedure call or argument)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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