+ Reply to Thread
Results 1 to 10 of 10

Adjusting Macro to be more dynamic for additional columns

  1. #1
    Registered User
    Join Date
    07-03-2010
    Location
    london, england
    MS-Off Ver
    Excel 2004
    Posts
    27

    Adjusting Macro to be more dynamic for additional columns

    Hey, below is the code I have to combine entries that have the same column A & B combination. It is based on the fact that I have 11 columns of data (13 columns total). I was wondering if there is a way to make it more dynamic, so if there are more or less columns of data it can adjust for that. I have marked the areas that would need to be changed.


    What the Macro does is basically do a SUMIFS statement to the right of the data, copy the values back and then erase duplicate rows,
    A:M is the 13 original columns (2 title columns, 11 data columns), I'm thinking something like a column count (if that exists?)
    N is the first free column (13 + 1) (column count plus 1)
    X = (13+13-2) (column count, plus column count, minus 2)

    Please Login or Register  to view this content.

    Thanks for the help!
    Last edited by minimacros; 07-19-2010 at 04:28 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjusting Macro to be more dynamic for additional columns

    A sample workbook to go with would really help.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjusting Macro to be more dynamic for additional columns

    Maybe this will do what you want, stab in the dark:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-03-2010
    Location
    london, england
    MS-Off Ver
    Excel 2004
    Posts
    27

    Re: Adjusting Macro to be more dynamic for additional columns

    Macro runs well. Since there are 2 columns of title data I adjusted your code at the following line
    Please Login or Register  to view this content.
    I'm curious as to why you changed the code for erasing duplicate lines.
    You have:
    Please Login or Register  to view this content.
    I had:
    Please Login or Register  to view this content.
    I notice how yours sets DelRNG as the area to be deleted and then just deletes it all at the end. Is there an advantage to this, this part of the Macro was never the slow part.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjusting Macro to be more dynamic for additional columns

    Heh, deleting an unknown number of rows all at once COULD be far faster than one by one deletions, so now you see another technique. Whenever I do mass deletions, this is what I use currently out of habit.

    General rule of thumb I try to go by...touch the sheet itself as infrequently as possible. That's why I also tried to show you have to isnert your formula directly into your target range instead of fillright, fill down.
    Last edited by JBeaucaire; 07-19-2010 at 03:14 PM.

  6. #6
    Registered User
    Join Date
    07-03-2010
    Location
    london, england
    MS-Off Ver
    Excel 2004
    Posts
    27

    Re: Adjusting Macro to be more dynamic for additional columns

    Thank you for your help JBeaucaire, I am new to making useful Macros and you have been a great help. Seeing your method of using numerical columns has helped tremendously. I'm sorry for not having posted a good sample workbook, I should have taken the time to do that. The reason I was eager not to, is so I could see how to adjust the macro while purely working with columns in a numerical fashion of some sort.

    I am planning on buying a VBA book (for excel '07/'10). Would you recommend the following book, or something else? http://www.amazon.com/Excel-Power-Pr...9566857&sr=1-1

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjusting Macro to be more dynamic for additional columns

    I can't recommend any particular book, I use the internet and online resources for researching/learning.

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  8. #8
    Registered User
    Join Date
    07-03-2010
    Location
    london, england
    MS-Off Ver
    Excel 2004
    Posts
    27

    Re: Adjusting Macro to be more dynamic for additional columns

    Sorry to revive an old thread, I was just curious why you set the DelRNG in beginning and how you know this won't erase the last row?
    Please Login or Register  to view this content.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjusting Macro to be more dynamic for additional columns

    Quote Originally Posted by minimacros View Post
    Sorry to revive an old thread, I was just curious why you set the DelRNG in beginning and how you know this won't erase the last row?
    Please Login or Register  to view this content.
    It absolutely WILL erase the last row. I've seldom seen data ranges that fill the entire spreadsheet all the way to the last row where the OP didn't mention that fact, it's usually already presented a whole slough of additional problems needing solving, too.

    So, 99 times out of 100 this is a safe technique.

    Why do I do it? It's a personal preference. I call it 'seeding' a deletion-range with the first value from a cell outside the data range. The point of the one line of code is to keep the Set DelRNG command that comes later to a simple UNION() method, possible because the DelRNG already exists.

    If I didn't do that, then inside EVERY loop I would be testing to see if the delRNG exists or not before adding to it, which is potentially 1000s of additional calculations. I can eliminate all of those by seeding the range once before I start into the loop.

    As I said, it's an efficiency/personal preference thing.

  10. #10
    Registered User
    Join Date
    07-03-2010
    Location
    london, england
    MS-Off Ver
    Excel 2004
    Posts
    27

    Re: Adjusting Macro to be more dynamic for additional columns

    Thanks!!!!!

+ 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