+ Reply to Thread
Results 1 to 4 of 4

Compile Error Constant Expression Required

  1. #1
    Registered User
    Join Date
    11-13-2011
    Location
    San Ramon, Ca
    MS-Off Ver
    Excel 2003
    Posts
    6

    Compile Error Constant Expression Required

    This might be a little confusing so please bear with me. I have excel sheets that contains data I want to use to update an Access Database. The sheets containing the data do not contain the macro I want to run. I don't want them to be macro enabled sheets. So I created a simple excel sheet that is macro enabled and contains the macro that reads in the data and updates the DB. I have the user open the excel sheet containing the macro then open the sheet containing the data. The macro from the first sheet then shows up on the data sheet when the macro tab is clicked. I want to make the reference to the access database relative so if the excel sheets and the data base are put on a different computer and different directory it would still work. Note: The excel sheet with the macro and the database WILL be in the same directory, but the excel may not be in the same directory.

    Below is the code I have tried but I keep getting a Compile Error: Constant Expression Required where the word .Path is highlighted. The other concern I have is that if the excel sheet with the data IS in a different directory that it will not get the path from the macro excel sheet but from the path of the data excel sheet. I have also upload all of the code I am using in case someone need to to see the whole thing. I hope someone can help me.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Compile Error Constant Expression Required

    Change this.

    Please Login or Register  to view this content.
    to this

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,522

    Re: Compile Error Constant Expression Required

    Const variable MUST be constant, no variable/function/method etc can be used
    So, need to change to
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-13-2011
    Location
    San Ramon, Ca
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Compile Error Constant Expression Required

    I tried just changing myPahth = ThisWorkbook.Path and I get the same Compile error only now it highlights myPath.
    I then tried the second sugguestionf fro Jindon about removing Const in the command Const DB_Location = myPath & "\MBM_Loans.accdb" However, when I removed the Const I then had another compile error stating that DB_Location was not defined (Variable not defined) so I added Dim DB_Location as String it worked even if the data sheet was in another directory.

    Below is the code that finally worked.
    Please Login or Register  to view this content.
    Thank for the everyones help.
    Last edited by dloskot; 10-10-2016 at 02:54 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compile Error: Expected expression
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2016, 02:33 PM
  2. [SOLVED] UDF gives compile error: expected:expression
    By adh2 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-10-2015, 05:37 PM
  3. [SOLVED] "ByRef:mismatch" and "Constant Expression Required" Errors
    By Mishil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-10-2015, 06:47 PM
  4. Compile error (Constant expression required) when using macro twice
    By flunzy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-02-2014, 10:35 AM
  5. [SOLVED] Compile Error: Expected: expression
    By Wheelie686 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2013, 08:51 AM
  6. [SOLVED] Compile error: Constant expression required error when merging two Codes
    By Kezwick in forum Outlook Programming / VBA / Macros
    Replies: 8
    Last Post: 06-26-2013, 09:32 AM
  7. [SOLVED] Compile Error: Expected Expression, Syntax Error
    By gjohn282 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2012, 11:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1