+ Reply to Thread
Results 1 to 10 of 10

My code is not dynamic; it has a pre-defined range. Possible to change code?

  1. #1
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    My code is not dynamic; it has a pre-defined range. Possible to change code?

    Hi all,

    I was inspired by some help I received on here earlier today to fix some code that was concerning me because it relied on a hard coded column number. I have a similar problem with this code where I have hard coded in the range of the data, but a more robust solution is that the code would search for the correct end of the range.

    Basically, I need to sort some columns themselves in Alphabetical ascending order. The code below does that. What I didn't know how to do is to tell it where the range needs to end, so I hard coded it. A better solution would be for the macro to know where to end the range by searching for a string and then offsetting the range 1 to the left of where it found that string.

    In Row B, the first column header which should NOT be included in range has the name "# Program Placeholder". I need the code to find that header name, and then define the range as being offset 1 column to the left of it. Currently, "# Program Placeholder" is in column BG (BG1) and so I have hard coded the range to be B1:BF1

    Is this possible?

    Please Login or Register  to view this content.
    If this is possible, I have another one that is almost the same need. The hard coded range BEGINS where "# Program Placeholder" is located and then I have it ending in some far far away column. An even better solution for this would be for the code to find where "# Program Placeholder" is and then set the beginning range for that cell, then find the last column where row 1 has data in it and set the end range for that cell.

    Please Login or Register  to view this content.
    Thanks for your time and expertise!

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    from what i can tell all you need to know is how to find the last column? and possibly last row?

    this site should be able to help
    http://www.rondebruin.nl/win/s9/win005.htm

    Range("B1:BF100") would become Range(cells(1,2),cells(100,lastcolumn))
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    Hi Humdaingaling,

    Thanks for taking a look at my problem. My last column actually is much further than BF. However, from column BG and beyond, I sort those columns separately.
    1. First I need to sort Area 1 (columns B:BF) A to Z, but as time goes on, BF may extend more columns to the right.
    2. Then, I need to sort Area 2 (BE:LJ) A to Z after that. Right now the end of the range is LJ, but the columns keep growing. Also, if area 1 was to grow more columns to the right, then I need Area 2's range to begin where Area 1 ends.

    Area 1, as I mentioned in the OP, needs to end at the column before the code finds "# Program Placeholder", and Area 2 needs to begin at "# Program Placeholder" and then end in the last column with data.

    Not sure if this made it any more clear?

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    not quite sure if i understand you but i mocked up some data

    see if it help you identify what you need?

    its basically to find the range...once you have the range you should be able to do the sorting bit
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    Quote Originally Posted by humdingaling View Post
    not quite sure if i understand you but i mocked up some data

    see if it help you identify what you need?

    its basically to find the range...once you have the range you should be able to do the sorting bit
    Your data mockup more or less describes what is going on exactly.

    It is finding for the first part, B1:BE50 correctly. This is the expected behavior.

    The 2nd part is finding B50:LJ58. This is not what I expected. The second part should find BF1:LJ50

  6. #6
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    I got the expected ranges by making some slight adjustments, here:

    Please Login or Register  to view this content.
    I am trying to figure out how to incorporate this into my existing code now...

  7. #7
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    I managed to get it working by taking some of your code and adding in a few new letters to reference. I am sure this isn't a great way to do it, but it is working at least. I know that J isn't used anywhere anymore, but I kept it anyways because maybe I will use a similar code for something in the future.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    thats fine
    J was just to use as an example anyway

    looking at your code i don't think i can optimise it anymore then it is
    except to say...there is no need to use "with" if there is only 1 row of code
    but that is just nit picking

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  9. #9
    Registered User
    Join Date
    04-30-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    Humdingaling,

    Thanks a ton for the help, this actually helped me to learn a bit more about how I can construct VBA for the future.

  10. #10
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: My code is not dynamic; it has a pre-defined range. Possible to change code?

    Sure thing not a problem

    Cheers
    Hum

+ 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] Need help with Dynamic Range in my code.
    By moonsaga in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2014, 01:08 PM
  2. [SOLVED] Code for a defined range which has been added to
    By Tishmimi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-27-2012, 08:03 PM
  3. Change code so that Dynamic ranges include sheet name
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-02-2011, 03:51 PM
  4. VBA code for dynamic name range
    By weii in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2007, 07:36 PM
  5. [SOLVED] Need code to replace part of a range within a formula with a defined name
    By Jeff in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2006, 06:10 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