+ Reply to Thread
Results 1 to 6 of 6

Make variable Goto command

  1. #1
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Make variable Goto command

    I have this sub :
    Please Login or Register  to view this content.
    So variable a is coming from another sub and works (checked with msgbox).

    a is equal to carrier1, carrier2 or carrier3.

    Now I wanted to, based on what vairable a is, directly go to the correct VBA part.
    But it is not working like this.
    Giving me label not defined.

    Is there a way around outside working with IF's?

  2. #2
    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: Make variable Goto command

    There are exceptions, but a very good rule of thumb is: don't use Goto. It is certainly not necessary here when you could use any number of options from If..Else to Application.Run. If you need to run different code based on an argument, you should almost certainly be calling different routines, not having all the code in one.

  3. #3
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Make variable Goto command

    Hmm ok rorya.
    I understand what you say about using separate routines.

    However in this case they (i my opinion) do belong to eachother.

    Bit more background.
    The code will copy data from a worksheet (in the same worbook) to another worksheet (in the same workbook).
    Now there a 3 scenario's how to filter the source data and copy the data.

    I want to keep these 3 parts together so its more clear to understand what the macro does (in the 3 cases).

  4. #4
    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: Make variable Goto command

    That still doesn't sound like a scenario for Goto. It sounds like one filter routine that takes different filtering parameters, and if you only have three options, a simple If...Then or Select Case would suffice.

  5. #5
    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: Make variable Goto command

    Purely for academic value, you could do this:

    Please Login or Register  to view this content.
    in much the same way that you can use On Error Goto -1, but you really shouldn't.
    Last edited by rorya; 07-09-2018 at 10:19 AM.

  6. #6
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Make variable Goto command

    Thanks rorya

+ 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. Replies: 2
    Last Post: 07-02-2015, 07:35 AM
  2. [SOLVED] GOTO sucks, but can I "GOTO Variable"?
    By smpita in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-25-2014, 03:14 PM
  3. IF Statement - GOTO Command
    By The Skipper in forum Excel General
    Replies: 2
    Last Post: 12-30-2012, 09:26 AM
  4. Command Key for GOTO command and relative reference
    By riwiseuse in forum Excel General
    Replies: 3
    Last Post: 10-18-2010, 01:37 PM
  5. Goto command
    By ledzepe in forum Excel General
    Replies: 3
    Last Post: 12-11-2006, 03:43 PM
  6. Goto Command in VBA
    By Ijit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2006, 04:22 PM
  7. [SOLVED] USING A GOTO COMMAND IN EXCEL
    By gscar2005 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-09-2005, 02:09 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