[VBA] Compile Error - Expected Function or Variable

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I have been getting a Microsoft Visual Basic for Application: Compile error: Expected Function or Variable message when trying to write and test a new module that I am working on.

The Module has 1 function and 1 sub (listed below) . When I try to run the sub (doArchive()) I get the Compile Error. When I run the Function in the immediate window it works, no errors. Can someone please help me figure out why.

Thanks

Code:
Option Compare Database
Option Explicit

Public Function NeedArchive() As Boolean

    Dim CurrentDateTimeStamp As Date
    CurrentDateTimeStamp = Now
    
    Dim MaxDateTimeStamp As Date
    MaxDateTimeStamp = Nz(DLookup("Max([ArchivalTimeStamp])", QUICK_Archive_Location, "[IncomingTermCode]='" & quick_termcode & "'"), #1/1/1900 12:00:01 AM#)
    
    Debug.Print CurrentDateTimeStamp
    Debug.Print MaxDateTimeStamp
    Debug.Print DateAdd("n", QUICK_Archive_Threshold, MaxDateTimeStamp)
    
    NeedArchive = DateAdd("n", QUICK_Archive_Threshold, MaxDateTimeStamp) < CurrentDateTimeStamp

End Function

Public Sub doArchive()
  MsgBox "Got Here"
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Trying Debug | Compile on your code yields the message you cite, and highlights QUICK_Archive_Location.
Excel is compiling the whole module.
If you just try to run doArchive, then only that part of the code is compiled and it works.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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