+ Reply to Thread
Results 1 to 20 of 20

Simplifying Code Using For-Next Loop

  1. #1
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101

    Simplifying Code Using For-Next Loop

    Looking to simplify the following code possibly with For-Next Loop.

    Please Login or Register  to view this content.
    Where the My Subs are private sub procedures. Thanks.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This question was also asked at
    http://www.mrexcel.com/forum/showthread.php?t=328091
    There are a couple of options there, both use Application.Run
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try this....

    Please Login or Register  to view this content.
    rylo

  4. #4
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    thanks for the response. What if the sheets were named (ex. "A", "B", "C" etc...) instead of numbered?

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Does the macro have the same suffix as the sheet (ie A,B...) or does it still have the number suffix?

    rylo

  6. #6
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    sorry, but i tried to give a generic example in the first code section. code looks more like this. thanks.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    You can convert numbers to alpha characters using the Chr VBA function. Using Rylo's code above, as well as your example for sheets A-D, you could use:

    Please Login or Register  to view this content.
    Chr(65) = "A"
    Chr(66) = "B"
    etc.

    HTH

    Jason

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    How much simpler could it be?
    Please Login or Register  to view this content.
    ... would be more like obscure than good practice.

  9. #9
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    What if subs are private?

    Please Login or Register  to view this content.
    doesnt work the same as

    Please Login or Register  to view this content.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    The first example doesn't work at all, for the same reason you can't do this:
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    looking to call the sub (in arr2) which corresponds to the correct sheet(arr1), however error message says that a sub, function or property is expected after the call command. thanks.

    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try changing
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

    actually shouldn't it be

    Please Login or Register  to view this content.
    rylo

  13. #13
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    rylo the first part of your suggestion works as far as activating the sheets however the second array (arr2) is an array of private subs which for some reason arent called in the examples above.

  14. #14
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Being lazy here - can you put up an example workbook with the subs, sheets etc.

    rylo

  15. #15
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    hey rylo-- i have completely solved my problem for this post. i re-worked it more like your most recent post and got it to work. Thanks again.

  16. #16
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    how bout this one. this is a simplified version.
    Please Login or Register  to view this content.
    i tried to do the standard concatenation but the compiler didnt like the fact that the new line started with quotes.

    Please Login or Register  to view this content.
    didnt work for me.

  17. #17
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    i tried to add this code to the previous post to eliminate the compile error but, i get new compile error: syntax error. any thoughts. thanks.

    Please Login or Register  to view this content.

  18. #18
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    VB does not treat strings resulting from concatenation as variables.
    VB does not treat strings resulting from concatenation as properties, methods, functions, collections, or objects.

    The approach you have been trying throughout this thread (refering to the argument a_1 as "a_" & "1") is fatally flawed.

    If you describe what you are trying to do, a successful approach (probably involving arrays) might be found.
    Last edited by mikerickson; 07-12-2008 at 07:07 PM.

  19. #19
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    mike--

    is there any way to reduce the redundancy then? thanks. bud.

  20. #20
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You should look into arrays.
    The code above could be done like this:
    Please Login or Register  to view this content.
    This could be further simplified, to one loop
    Please Login or Register  to view this content.
    Starting with arrays and using For..Next loops makes it easier to see this version.
    Please Login or Register  to view this content.
    Unless you need myRange or formulaString later in the routine, this would be the way to go.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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