+ Reply to Thread
Results 1 to 12 of 12

Refining Macro to Consolidate Columns into one Sheet

  1. #1
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Refining Macro to Consolidate Columns into one Sheet

    Hi All,

    Been reading bits and pieces around here (and various other sites) as I get myself back up to speed in VBA scripting (it's been several months since I've had to do anything and I'm pretty rusty already).

    Was hoping someone would be kind enough to assist me in refining down my code to be shorter and more to the point?

    Specifically, I would really like help in replacing my cheatsheet (Sheet19 in the code) I used to replace the Column Number with a subsequent Column Letter (Using Function: "ColLetterFromNo"... I think R1C1 might be an option for me??) and figuring out a way to further variable-ize the code so that it will eventually work nearly automatically. Also, I've been having the most difficult time getting the clear portion of the macro to work on the Import sheet, so I resorted to having the macro use the CountA WorksheetFunction again (there's a better way, I am certain of this, but I can't seem to get it just right...) to make it work.

    The point of this sheet is to check the Headers on my "Import" Sheet, loop through the subsequent sheets and pair up those Headers with that of the current sheet it loops into, determine the final row in the sheet (Note: Final Row of a specific column, I maintain an arrayed ID # throughout the entire sheet, which prohibits normal last row lookups) and then copy those columns to the Import Sheet. As it continues through, it appends the data rather than replaces as well. Eventually I will be making this entire sheet copy its data to another workbook to perform a SQL database update.

    I've spent about 2 days getting to this point.

    Please Login or Register  to view this content.
    Notes: I have had to comment out anything in the 'E' category as I have not set up my data sheet to handle everything in that column appropriately yet. I cannot share the workbook (maybe I can rig up a small test if needed) but I will be handling up to a couple thousand rows per sheet potentially and the column layout of each sheet is vastly different, fortunately I am only pulling the similar data from each sheet to my "Import" worksheet which makes it a little simpler.


    Much appreciation to whomever is willing to lend me a hand here.




    LW
    Last edited by lonewolfe2015; 11-26-2012 at 08:09 PM.

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

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hi

    Maybe you should be using the cells approach to the range rather than the range approach. Given that your aloop type variables come back with a number then maybe you should do something like
    Please Login or Register  to view this content.
    Keep away from variables that are keywords - in this case count is a keyword - so make your variable count into something like cnt or mycount...

    Make sense?

    If you really want to keep the range approach, and so need to have the column alpha coding then you could something like
    Please Login or Register  to view this content.
    (to use your own method of getting it) to put the column descriptor to a variable.

    HTH

    rylo

  3. #3
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Thanks for that rylo.

    Below is my updated code using your suggestions and changing some of the styling to use the Cells() syntax. I finally got it to work with the Column Numbers by changing some of the variables to be 'Long' instead of Strings after doing a bit more of reading and testing. Then I also modified the Import sheet clearing code to be more effective, I think this is a much better script than the one from before.

    Is there perhaps a way I can modify the script to handle a variable number of headers from the Import Tab (I used A-Fimps to define the individual headers, then A-Floops to search for them in each sheet) without assigning each header to a variable and looping through all of them? In the future I may need to import more information and I'd rather not copy the code continuously (or leave directions to do so) and simply have it parse out each cell in the A1 row/header and search for each one in the subsequent sheets.

    Also, can excel loop through the Sheet codenames rather than Sheet location names? Example, I name all of my extra sheets Help1-4 for instance, my data will be Sheet1-XX, can it loop through Sheet1, Sheet2, Sheet9... using their assigned codenames?

    Please Login or Register  to view this content.
    Last edited by lonewolfe2015; 11-20-2012 at 11:53 AM.

  4. #4
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Weeeeird problem... I've been toying with the code and cycling through items that have been getting added by myself and another.

    Everything is working fine, however when I try to have it aggregate data beyond the 1028th row on the "Import" sheet it causes a Run-Time 1004 error (Application-defined or object-defined error) and references this code...

    Please Login or Register  to view this content.
    Remove what would be the 1029th row on the sheet and run the script again, everything runs smoothly.

    Whenever it errors, somehow towards the end of the code (the 7th sheet) it starts freaking out and copies the 2 lines of information 10,000 times, then on the 8th sheet it copies the data 89,000 times. These data points being around the 980th line, not the 1028th.

    I can manually bypass it by running it in two parts (i = 3 to 7 and 7 to 13) but not combined.

    Any suggestions?

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

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hi

    I'm pretty sure that you should be able to do all this with a couple of loops. Retain your current i loop as an outer loop, then use an inner loop to cycle through the cells in A1:F1 and action each one in turn. You could have it self determine the last column to action pretty easily so it doesn't have to be fixed.

    Re the second problem, I'd like to see an example file that replicates the problem. My first place to look would be at the raw data as there may be something in that which is giving you the problem.

    Put up an example file so we can see what you are working with and I'll have a look with a view to making your code "loopy"!

    rylo

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

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hi agin.

    Took a bit of a punt on some of your structures, and how I interpret your code and think this may get close.

    Please Login or Register  to view this content.
    rylo
    Last edited by rylo; 11-20-2012 at 06:39 PM.

  7. #7
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hopefully this uploads alright.

    I've got a lot more data in sheets and you'll have to work around the blank fields I just created. If you want to just add fake data I suppose it should work fine for testing purposes, the fields never hold more than 24 characters (e.g. Supplier: XYZ Company, Item #: 5556667AB). I kept all of the dimensions so it would still be helpful to visualize what I am doing. And blank cells will always contain a "-" to ensure that the field has some sort of data within it.

    In the future it sounds like I'll be passing this form off to someone very computer illiterate while means I need to do as much as I can to simplify the code and make it very flexible. As it stands my current code bugs out a few too many times.

    I tried your above macro and it failed when setting the rng variable, I'll look at it some more throughout the day and update this post if I figure anything else out.

    Thanks for all of the help so far rylo.
    Attached Files Attached Files

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

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hi

    I just managed to leave out one tiny full stop. Damn. Modified code below. Note that I've only made it run to sheet 4 as you don't have all 13 data sheets in the example workbook. Put it in the example file, select the first sheet and see how it goes.

    Please Login or Register  to view this content.
    rylo

  9. #9
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Thanks, will test on Monday as I don't have remote access and we have the holiday weekend in the states. Couldn't do anything with the data sheet today as I was needed elsewhere.

    Hopefully your code will help negate the previous error I was having with a larger amount of data. I appreciate all of the help you've been giving me and will definitely be spending some time understanding this code and adapting it to other purposes in the future.

  10. #10
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Wow, impressive macro. My only problem now is that it addresses what range to copy based on if the cells contain a value or not for each column. I need a static number to be copied on each sheet and a variable number to be copied from sheet to sheet, if you understand what I'm saying. When I run it for all of my sheets, from 1 to 998 (the length of my table) for the ID #'s, descriptions and suppliers because I maintain a dash in each column. So those fields should be omitted from being copied despite containing a value of some sort.

    We could make use of the Item # field to create the static column length (as it will never contain a value if actually empty).

    Thanks for the help to this point, I can't believe what I wrote was able to be simplified so far down yet still work on a variable number of fields. It's a much quicker script now too.

    E: Here's what I've been trying/tested. It's been working all day for me, any recommendations on the changes I made? I tested adding new columns and those appear to have appended data perfectly as well.

    Please Login or Register  to view this content.
    Last edited by lonewolfe2015; 11-26-2012 at 04:10 PM.

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

    Re: Refining Macro to Consolidate Columns into one Sheet

    Hi

    think I can follow what you are doing, and it looks OK to me.

    rylo

  12. #12
    Registered User
    Join Date
    11-16-2012
    Location
    Nowhere
    MS-Off Ver
    Excel 2007, 2013
    Posts
    9

    Re: Refining Macro to Consolidate Columns into one Sheet

    Thanks rylo, really appreciate the help. Made sure to rep you for the support.

+ 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