+ Reply to Thread
Results 1 to 11 of 11

Compile Error Expected Function or Variable

  1. #1
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Compile Error Expected Function or Variable

    Hi,

    I'm fairly new to VBA so please bare with me. I've basically created this macro which print screens and saves the file into pdf looking up which sheet and range in a tab that i created. I'm however getting this compile error with the "Sub test()" turning yellow:

    Please Login or Register  to view this content.
    Any ideas?
    Last edited by shiser; 05-22-2014 at 09:19 AM.

  2. #2
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Compile Error Expected Function or Variable

    Sheet35 is a variable you did not declare what is in it.

    Do you means Sheets("Sheet35") or Sheets(35)?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Compile Error Expected Function or Variable

    Used in that fashion, Sheet35 must be the CodeName of a sheet, which is what appears to the left of the sheet name in the Project Explorer window. In a new workbook with two sheets, you would see

    Sheet1 (Sheet1)
    Sheet2 (Sheet2)

    Change the name of Sheet1 to Bob, and you'd see

    Sheet1 (Bob)
    Sheet2 (Sheet2)

    After doing that, you could refer to the first sheet as either Sheet1, or Worksheets(1), or Worksheets("Bob")
    Last edited by shg; 05-21-2014 at 01:02 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Compile Error Expected Function or Variable

    I've called the worksheet "test" and I'm getting the same error. I've adjusted it as:

    Please Login or Register  to view this content.
    I've also tried:

    Please Login or Register  to view this content.
    which I believe is the same thing. I still get the same error annoyingly

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Compile Error Expected Function or Variable

    Quote Originally Posted by shiser View Post
    I've called the worksheet "test" and I'm getting the same error. I've adjusted it as:

    Please Login or Register  to view this content.
    I've also tried:

    Please Login or Register  to view this content.
    which I believe is the same thing. I still get the same error annoyingly
    The first line should never generate a compile error; it would generate a runtime (subscript) error if there is no worksheet named test in the active workbook.

    The second line is missing a closing quote after test.
    Last edited by shg; 05-22-2014 at 04:17 PM.

  6. #6
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Compile Error Expected Function or Variable

    I'm confused then because I'm getting a compile error.

    The second code was written in heist on the forum but I've had the correct formula in the macro.

    Wouldn't the worksheet part of the code turn yellow if that was the issue? Just to clarify, the "Sub test()" is getting the error and turning yellow.

  7. #7
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Compile Error Expected Function or Variable

    I think I know what the problem is! Its this code:

    Please Login or Register  to view this content.
    Basically, what I want it to do is look up the sheet dependent on what I have in my tab.:

    Please Login or Register  to view this content.
    and the lookup cell contains for example:

    Please Login or Register  to view this content.
    Which is located in cell C4. With my Macro stating i = 4 and sTab location being (i, 3), it should be picking up the correct cell I think.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Compile Error Expected Function or Variable

    Do you mean:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Compile Error Expected Function or Variable

    "subscript out of range"

    So close to figuring this out!

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Compile Error Expected Function or Variable

    That means sTab does not contain the tab name of one of your worksheets.

  11. #11
    Registered User
    Join Date
    01-10-2014
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    57

    Re: Compile Error Expected Function or Variable

    Quote Originally Posted by romperstomper View Post
    Do you mean:
    Please Login or Register  to view this content.
    This worked. I mistakenly had quotations in this line. Thanks!

+ 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 variable or procedure, not module
    By kaseyleigh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2015, 03:46 PM
  2. [SOLVED] Compile error expected function or variable (on protect method)
    By Tirren in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2013, 03:22 PM
  3. Mac issue (please help) "Compile error: Expected Function or variable"
    By m1notaur in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-22-2011, 06:00 PM
  4. Compile Error:- expected function or variable problem
    By skin.uk1 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-08-2011, 02:56 PM
  5. [SOLVED] Compile error: Expected Sub, Function, or Property
    By Cloudfall in forum Excel General
    Replies: 3
    Last Post: 08-30-2005, 01:05 AM

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