+ Reply to Thread
Results 1 to 11 of 11

GoTo - where you previously were?

  1. #1
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    GoTo - where you previously were?

    Hi all,

    I have a long code which inserts formulas into cells, based on the value of another cell, but because of a third one which tells us the region I would need to repeat the formulas part (5 formulas for 11 countries) 5 times, so instead I decided to say, if my condition is met (so the formulas need to be added to the cells) then "GoTo Formulas" and I just don't know how to tell excel, that once the correct formulas are in the cells, it should return to where it was in the code previously, cause I have the "GoTo Formulas" 5 times in the code and I would have 5 different return points depending on where it jumped out from.. (all of this in a userform btw)

    Here is a mock-up of the code:

    Please Login or Register  to view this content.
    Can someone help me with this?

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

    Re: GoTo - where you previously were?

    General rule of thumb is: don't use Goto other than in an On Error Goto line.

    You should put your Formulas bit into a separate routine and just call that from the relevant section(s) in the code you posted.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,481

    Re: GoTo - where you previously were?

    Make Formulas a separate subroutine or function and just call it. You'll need to pass it the variables or set them up as Public variables.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    Re: GoTo - where you previously were?

    I can do that but I still don't know how to tell Excel to then return to the next line from where it called the "Formulas_Sub".

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

    Re: GoTo - where you previously were?

    It will do that automatically.

  6. #6
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    Re: GoTo - where you previously were?

    Oh yeah? I'll give it a go.

  7. #7
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: GoTo - where you previously were?

    Hi, Another possibility is the use of the "Gosub" construct, it is functionally similar to the other solutions in that it avoids "goto" but keeps the code in the current routine so there is no issue variables scope.

    Both methods will return to the statement following the "call" or "gosub"

    Just an alternative approach to consider

    Cheers

    Jmac1947

    1. Please consider clicking on the * Add Reputation if you think this post has helped you
    2. Mark your thread as SOLVED when question is resolved

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

    Re: GoTo - where you previously were?

    Gosub should also be avoided in my opinion. It’s like GoTo but worse in that you’ve recognised an opportunity to refactor your code but then not done it.

  9. #9
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: GoTo - where you previously were?

    You are right. Those of us who came from a green screen mainframe programming background tend to work in a top down approach and simply identify the opportunity for "sub routines" and worry about coding the actual work later. This makes the gosub an attractive option to begin with until we get more experience in functions, variable scope etc. (it is hard enough learning a new way of thinking as it is )

    I don't tend to use them now as I am more comfortable with VBA

  10. #10
    Forum Contributor
    Join Date
    08-29-2017
    Location
    London England
    MS-Off Ver
    2016
    Posts
    194

    Re: GoTo - where you previously were?

    Sorry it took me a few days to test it hence I didn't mark this solved yet, but

    Make Formulas a separate subroutine or function and just call it. You'll need to pass it the variables or set them up as Public variables.
    This works perfectly for me

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,481

    Re: GoTo - where you previously were?

    You're welcome.

+ 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] 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
  2. [SOLVED] Hyperlink to the cell from where it was previously
    By hanijohn in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-07-2013, 10:10 AM
  3. DONKEYOTE - IRR Previously worked on
    By a.mack123 in forum Excel General
    Replies: 2
    Last Post: 08-31-2010, 12:25 PM
  4. Activating previously used workbook
    By Ozgur Pars in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-10-2006, 02:00 AM
  5. Clearing what you have previously selected
    By T8RSP in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-07-2005, 06:30 AM

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