+ Reply to Thread
Results 1 to 8 of 8

Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error.

  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    Anchorage, Alaska, USA
    MS-Off Ver
    Office 365
    Posts
    113

    Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error.

    Hi,

    Is it just me, or is this normal? Does WITH not travel through a GOSUB routine?

    I kept getting an "invalid or unqualified reference" error on a line where I used .cells(row, column).value in a Gosub routine. the keyword CELLS kept highlighting along with the pop-up error. I couldn't figure it out. I've used WITH, END WITH, and dot-references often. I fixed the error, but why it's fixed is bizarre...to me. I'll post a modified workbook if I need to, but I'll try to explain because maybe this is just some common thing I haven't come across.

    So, normally a dot-anything reference needs a WITH and an END WITH.

    This works:
    Please Login or Register  to view this content.
    This doesn't because there's no WITH / END WITH to qualify the dot-cells command:
    Please Login or Register  to view this content.

    So, I was surprised when this gave me the above error (highlighting the WITH's in red. The error in blue:
    Please Login or Register  to view this content.
    (In case you're thinking 'put the with/end with in the gosub', the actual code has more going on so having the WITH where it is makes sense...or I thought it did)

    To me, the WITH should have traveled into the Gosub routine and the x=.cells should have worked. But it didn't.

    Here's how I fixed it, but it was a shot in the dark, and I dont know why this works:
    Please Login or Register  to view this content.
    No more error. I had to re-state and essentially "nest" the WITH/END WITH to make it work.

    But why?!?!

    Totally confused.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,946

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    The With / End With needs to bracket any use of the . structure, so this should work


    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    Why are you still using GoSub?
    Rory

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    GoSub is an old spaghetti-code structure that is retained for backward compatibility. It has great potential to create havoc and should be avoided. Better to make it a Sub.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Forum Contributor
    Join Date
    04-03-2009
    Location
    Anchorage, Alaska, USA
    MS-Off Ver
    Office 365
    Posts
    113

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    Thanks Bernie.

    That's crazy to me. The END WITH at the actual end of the code use, not necessary execution-use, if I'm making sense. Thank you.

    Mind blown.

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,946

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    That's how a structured programming language needs to work. If you don't want to use With/End With, then use an object in place of the With, using code like

    Please Login or Register  to view this content.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    Quote Originally Posted by akedm View Post
    That's crazy to me. The END WITH at the actual end of the code use, not necessary execution-use, if I'm making sense.
    The With/End With structure is evaluated statically, not at runtime. When the parser reads the GoSub section it cannot determine what With is in effect to be able to resolve the .cells reference. The With/End With must lexically surround any references.

    A language could be designed to make With/End With evaluated at execution time but this would create all kinds of problems, and allow some really spectacular bugs.

  8. #8
    Forum Contributor
    Join Date
    04-03-2009
    Location
    Anchorage, Alaska, USA
    MS-Off Ver
    Office 365
    Posts
    113

    Re: Does WITH/END WITH travel into Gosub routines? Invalid or unqualified reference error

    I like it

    shtS object vs with/end with.

    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. [SOLVED] getting compile error invalid or unqualified reference
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2015, 08:36 AM
  2. [SOLVED] Getting error invalid or unqualified reference
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-20-2014, 08:15 PM
  3. [SOLVED] Getting error invalid or unqualified reference
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2014, 02:17 AM
  4. [SOLVED] Getting error invalid or unqualified reference
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2014, 11:31 PM
  5. [SOLVED] Invalid or Unqualified Reference Error
    By Trevasaurus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-28-2013, 11:44 AM
  6. [SOLVED] VBA Debug (compile error invalid or unqualified reference)
    By Kezwick in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2012, 04:38 AM
  7. Invalid or unqualified reference error using the .Find in vba
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-08-2011, 07:10 PM

Tags for this Thread

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