+ Reply to Thread
Results 1 to 13 of 13

GOTO sucks, but can I "GOTO Variable"?

  1. #1
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    GOTO sucks, but can I "GOTO Variable"?

    Hello!

    About a year ago I started writing a macro. Because of my coding nubiness, I used a lot of GOTO commands. Over the year, my macro has grown to be quite large and I have learned the errors of my ways. I am in the process of rewriting the code to remove GOTO from my code.

    As I rewrite this long macro into functions and subroutines, I still need to use some GOTO commands to connect to old code bits. I made a function called "DetectReport" that returns the GOTO label name. Can I use GOTO with a variable?

    Like I will have a variable (strReportName) that could have the values "REPORT1" or "REPORT2" or "REPORT3"

    And in my code I will have some labels that look like:

    Please Login or Register  to view this content.
    I want to do a GOTO strReportName

    Thank you!

  2. #2
    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: GOTO sucks, but can I "GOTO Variable"?

    Relevant thread here. Apparently not.

    One way:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  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
    44,428

    Re: GOTO sucks, but can I "GOTO Variable"?

    Short answer: good luck with that.

    http://www.vbaexpress.com/forum/show...Sub-GoTo-Point


    Regards, TMS
    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
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: GOTO sucks, but can I "GOTO Variable"?

    Thank you 6StringJazzer and TMS. I think I'll use select case for the time being.

  5. #5
    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
    44,428

    Re: GOTO sucks, but can I "GOTO Variable"?

    You're welcome. Thanks for the rep.

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: GOTO sucks, but can I "GOTO Variable"?

    Please Login or Register  to view this content.
    You could use DetectReport to load the names into an array, then loop through it, incrementing ReportNum and branching if the name meets some condition...
    Last edited by protonLeah; 06-24-2014 at 09:17 PM.
    Ben Van Johnson

  7. #7
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: GOTO sucks, but can I "GOTO Variable"?

    Thank you, protonLeah. I've never used GoSub. I think I get how it works from your example. I might be able to use it during the recoding transition.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: GOTO sucks, but can I "GOTO Variable"?

    Why not put the code for each report in it's own sub?
    If posting code please use code tags, see here.

  9. #9
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: GOTO sucks, but can I "GOTO Variable"?

    Norie, I intend to be doing that but there is a lot of code to parse and separate and my time per day to work on it is limited so I will be doing it in stages. First step was to break up my "If Range(whatevercell).value = UniqueThingOnTheReport then Goto ReportName" block at the start of the macro.

    Several pieces do similar tasks rewritten for the individual report. I'm going to try to make more generic functions that each report can use, for example saving the file to a folder for the folder with a naming convention specific to the report type. I'm doing this both for practical usage as well as learning.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: GOTO sucks, but can I "GOTO Variable"?

    If you are using Goto then it should be easy to separate out the code.

    For example this,
    Please Login or Register  to view this content.
    would become this.

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: GOTO sucks, but can I "GOTO Variable"?

    It's not going to be too difficult to break into subs but a little time consuming. I do want to rewrite several portions into generic functions, especially save handling and error handling. I was going to post my code in it's entirety for you to see what I mean but posts are apparently limited to 15000 characters.

  12. #12
    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: GOTO sucks, but can I "GOTO Variable"?

    You can attach your entire file.
    1. Under the text box where you type your reply click the Go Advanced button.
    2. In the next screen look above the reply box and click the "paper clip" icon. You will get a pop-up screen.
    3. In the pop-up, click Add then Select to select a file from your local drive
    4. Click the Upload button to upload the file
    5. Click Done to attach it.

    It will be displayed as an attachment underneath your post text. Alternatively as the last step you can click the Insert Inline button and a hyperlink to the file will be inserted directly into your post text in the spot where your cursor is set at the moment you click the button.

  13. #13
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: GOTO sucks, but can I "GOTO Variable"?

    Everything starts with a keystroke to run AutoReportResize(). code.txt

+ 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. GoTo compile error: "Expected: line number or label"
    By elfsprin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2011, 09:21 PM
  2. [SOLVED] Application.Goto Reference:="ActiveCell.Value"??? Whats wrong??
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-12-2006, 06:50 AM
  3. Alternative to "On Error GoTo 0" that takes macro to last command?
    By StargateFan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-25-2006, 01:10 PM
  4. [SOLVED] Can I place "On Error GOTO xxx" into module level?
    By Frederick Chow in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2006, 12:10 AM
  5. [SOLVED] Are there "GoTo/Jump" type of formulas to move between worksheets
    By LV in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2005, 11:05 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