Inefficient VBA code- takes literally days to run

  • Hi Gurus,


    I'm hoping you can help me improve a bit of code that I cobbled together as a VBA novice. The basic premise is to convert a matrix to a list. My matrix (Table 1) includes unique ID, personal info, unique key (same as unique ID) and assigned roles where an "X" matches the name (row) with the role (column). It is easy to create the reverse pivot table (Table 2) and the result is a table with Unique ID, Role, and 'X'. The code below goes back to Table 1 and stiches back personal info by matching unique ID (Table 1) with Unique Key (Table 2). It works - just VERRRRY slow as there will be 10-20k lines on Table 2.


    Table 1 (the matrix). In my code it is called 'RDBMergeSheet'


    Unique ID - Personal info - Unique Key (same as unique ID) - Roles (indicated with an X in the cell which aligns with the appropriate role column. There are multiple Xs)

    Table 2
    (the reverse pivot). In my code it is called 'Data Table'


    Unique Key - Role (now as a title). I filtered and removed all blanks. Note that the same unique key will appear for each role that that user has


    Goal


    I want to add the Unique ID's personal info from Table 1 for each Unique Key.


  • Re: Inefficient VBA code- takes literally days to run


    Hi Roy -


    Makes sense. Please find it attached. The code that is giving me the issues is in Module 4 - "Fill Info" - though I'm sure they all are pretty bad. Classic example of someone self teaching from recording and piecing together bits of code he's found on the internet. I needed to cleanse this of actual info, but there would be about 500-1000 'Champions' (managers) not a 1:1 match with End Users.


    Thank you so much for looking at this!


    Cheers,
    Tim


    forum.ozgrid.com/index.php?attachment/70623/

  • Re: Inefficient VBA code- takes literally days to run


    Days does seem excessive and not sure why it would be so slow. There are some general things you can do to speed up code, e.g.

    Code
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

    at the beginning of your code and then turn them on again at the end. Also, avoid selecting and activating as far as possible. Instead of

    Code
    Sheets("RDBMergeSheet").Select 
        LastC = LastCol(ActiveSheet) 
        LastR = LastRow(ActiveSheet)


    use

    Code
    LastC = LastCol(Sheets("RDBMergeSheet")) 
        LastR = LastRow(Sheets("RDBMergeSheet"))

    etc

  • Re: Inefficient VBA code- takes literally days to run


    Hi,


    I've just had a look at re-writing some of your code "as-is" - I haven't looked at the workbook though, see if this is any quicker.



    Also I don't know what your LastC() and LastR() functions look like - but if they're looping then they are probably the culprit - not this.


    Hope that helps,


    S O

  • Re: Inefficient VBA code- takes literally days to run


    Your example workbook does not contain the sheets referred to in the Fill_Info Procedure.


    If I knew what columns you are referring to I'm sure the code could be greatly simplified. Which column is the Unique ID?

  • Re: Inefficient VBA code- takes literally days to run


    Hey Roy


    Thanks for your continued help - sorry for the delay in getting back to you. If you put the file that I posted in a folder and go to the instructions tab - the first two macros (in buttons on that tab) will create all of the worksheets. The first one explodes out a matrix into lots of individual workbooks - one for each manager or 'Champion.' If you want to run it I'd suggest deleting the bottom 2/3rds of the list. The second one then pulls them back into one big list (RBGMergeSheet). I didn't bother to go into too much explanation of this earlier because those macros seem to work really well. The one that seems take so long is the 'Fill Info' macro which goes back to the merge sheet and pulls in the personal information.


    Unique ID is in Column A. Unique Key is in column M.


    Thanks others for your help - I'll go give them a try! LastR() and LastC() are just calling the LastRow() and LastCol()


  • Re: Inefficient VBA code- takes literally days to run


    My current work around - because filling in this info is taking SO LONG - is just to do vlookups in each of the columns which generates my list almost instantaneous. Do you guys think that might be a better approach? That is to just completely replace the 'Fill Info' maco with just one just pastes (or runs) a vlookup in each of the columns on the Data Table sheet?


    If so - I'd love some guidance on the best way (I mean most efficient in terms of running it) to do that? Goal would be on the Data Table to have a vlookup for the Unique Key back to the RDBMergesheet.


    Thanks!

  • Re: Inefficient VBA code- takes literally days to run


    Hi Stephen -


    Yes I tried it - but it errored out on the lastCol = LastC(merge) line. I don't know a whole lot about VBA, but I would assume that is probably because LastCol is already a Public sub (see my 2nd to last post)...not sure what the way is around that.


    If I can't improve my code, and go with the formula suggestion, I'd still like to automate that via some code - I can go hunting for the answer, but if anyone here wouldn't mind pointing me in the right way for how to use VBA to put a series of 'vlookup' formulas in, I'd greatly appreciate it.

  • Re: Inefficient VBA code- takes literally days to run


    Ah yes, that's just a typo. The function names should be as you posted (so LastCol instead of LastC) and change the variable names lastcol and lastrow.

  • Re: Inefficient VBA code- takes literally days to run


    Actually, I don't think it's inefficient code as much as an efficient method. I ran the first step and let it create 336 worksheets then stopped it.


    There is absolutely no point in having that many sheets, let alone workbooks. I would have a master template all data on one sheet as you have then use AdvancedFilter to generate reports for Champions as required.

  • Re: Inefficient VBA code- takes literally days to run


    Quote from royUK

    I would have a master template all data on one sheet as you have then use AdvancedFilter to generate reports for Champions as required.


    I really can't emphasise enough how much this advice should be heeded, Excel is an extremely powerful and capable tool if you use it the right way - let it do all the work for you!

  • Re: Inefficient VBA code- takes literally days to run


    Thanks a lot for that advice. Just as a bit of a background for why this tool creates so many worksheets/workbooks: We need to send out a list of employees to each manager in the company and have them provide input into who is on their team, and what roles they need. Essentially a bottoms up HR report.


    The parts for breaking out the individual workbooks and re-combining them work well (though maybe not in the template version I posted because it has waaaay more managers than we actually have ~200ish). The part that isn't working well is just copying the personal information from the merge sheet to the data table.

  • Re: Inefficient VBA code- takes literally days to run


    What you're explaining sounds to me like something you should perhaps be using a database for - at the moment you're sending chunks of data out to hundreds of places, and having the changes sent back from hundreds of places to then copy back to the original data.


    To me, it would make more sense to have all the data in one place centrally, and simply have all the managers access the relevant information at the source. You can easily restrict the data viewable by using a login and then they simply change the data in place, saving any copying/merging processes.


    In fact, royUK had a database form on his site that may give you some inspiration here - check the link in his signature.

  • Re: Inefficient VBA code- takes literally days to run


    yes! A database would I'm sure be much more appropriate - but out of my current skillset. I'll check out Roy's form. for now I think I've got a workaround.


    Thank you all for your time and assistance!

  • Re: Inefficient VBA code- takes literally days to run


    This is some code that I have used for years that will create individual sheets for each unique ID, then move the sheet to a new workbook


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!