+ Reply to Thread
Results 1 to 22 of 22

A better way to store/retrieve bulk data?

  1. #1
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    A better way to store/retrieve bulk data?

    Not sure whether this belongs in the VBA forum or here.

    I have a VBA script on one worksheet that pulls information from a second worksheet. (I won't go into details unless you guys want them.) Problem is, that second worksheet has over 150,000 rows of data, and I'm working hard to keep that number low. The script is run hundreds of times per day and can easily have dozens of lookups each time it's run. As each lookup takes 2-3 seconds, this slows down the whole process significantly.

    Is there a quicker way for me to store this information? A co-worker suggested MS Access, but I have no experience in it. Can my Excel sheet quickly and easily pull from an Access file? Or is there maybe a better way to store this much data?

    Thanks!

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    can you post some details? are you using lookup functions in the sheet, like vlookup() or Index(), or are the "dozens of lookups" happening in the code with .find() ??

    can you at least post some of the code? also, yes there are ways to connect excel directly to other databases. there are built in 'canned' microsoft queries you can hard-wire to the worksheet and you can also use ADO to query a datassource in VBA.

    you also have the option of using power-pivot for large sets of data if you are using excel 2010, that might be useful to you... but depends on what you're doing exactly.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    No problem. The macro is extremely long and exists on another network. I cannot paste the whole thing over. I can rewrite some of it, though.

    What it does:

    1) User copies a bunch of text to clipboard
    2) User goes to Excel and runs macro
    3) Macro pastes and does a bunch of nifty reformatting and such. One of the things it does is:

    Column B now has a bunch of numbers in it; one in each cell. One step of the macro is to look at each of those numbers and determine whether they appear anywhere in a separate sheet, called "Comments." This is the sheet with over 150,000 rows. Every time it finds that number, it pastes the entire row(s) into a cell in Column A on the original sheet, just to the left of the corresponding number (which is stil in Column B).

    For example, if Cell B5 has the number 93428, Cell A5 may look like:

    My favorite number is 93428.
    The number 93428 is how many jelly beans are in this jar.
    George owes Donald $93428 dollars.

    How it does this:

    It does this by creating a custom function. That function is:

    Please Login or Register  to view this content.
    The line in the macro that uses this function is:

    Please Login or Register  to view this content.

    As far as PowerPivot, I do not have access to it on the network that this spreadsheet is on. I can try to get the IT guys to add it if you think its the answer.

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    PowerPivot is probably not the answer here (doesn't sound like pivot tables will help you).

    However, your big time lag is having the code loop through every cell in the range. I would suggest altering the custom function to make use of the .find() method and the Union operation.

    Basically, you can greatly improve things by building a range that only contains 'hits'. You can then do what you will with the values in those cells. By doing this, instead of looping 150000 times... you will only loop for the number of matches.

    Would go something like this code below:

    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 05-28-2013 at 02:54 PM.

  5. #5
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    Thanks. However, I tried this and it is not throwing any matches in Column A. No errors, but no matches either; and I know there are matches. Here's the code I used. I have almost no real knowledge of VBA (I Google everything), so what I put in the last section may be wrong. I was trying to guess as best I could.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    oh man, i forgot a really important step in the code -- i edited my previous post and added that in.

    Set c = .FindNext(c)

    You should be getting matches with that line, you may have to change, what:=lookfor to something like what:="*" & lookfor &"*" but try just adding in the line i forgot before bothering with the "what" parameter (I'm pretty sure that find is already looking for the text anywhere within the cell value).


    Also, once you have the matches, you won't need to perform any logical checks on the range "matches" -- all the cells found will contain the text.

    So, just go with:

    Please Login or Register  to view this content.
    Offset(0,0) is also not necessary (means no offset).

  7. #7
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    Still isn't working. =(

    Does "hit" need to be defined somewhere?

  8. #8
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    Are you stepping through the code? Is the code making it into the IF statement at all ?

  9. #9
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    Ok, take a look at the attached workbook to test this out. I am confident this can greatly speed up your code...

    Since you can't upload an example, i put together the function and a simple test Sub to show it in action. Open the workbook, enable the macros and press the button to test the function.
    You can step through the code to see it working. Press Alt+F8 to see the macro, select "test" and press "Step Into". Press F8 to take each step.

    I have a message box pop up to show the results, and i also list them in the sheet in the A column. I think you had a different final step, but maybe this will be something you can edit to meet your needs.


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by GeneralDisarray; 05-28-2013 at 04:34 PM.

  10. #10
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    Quote Originally Posted by daedelous00 View Post
    Does "hit" need to be defined somewhere?
    Well, it's defined in the "dim" statements at the top of the function (Dim hit as Range). When you use a FOR EACH style loop in Excel, you are looping over a "collection".

    I don't have to define "hit" any more than with the Dim statement. When i reference a Range variable in that FOR EACH loop, Excel determines what "hit" should be interpreted as based on the variable type and the Collection I've given it (the collection is the other range variable "matches").

    Think of it like this: For Each member In collection --- that is the general syntax.

    Take a look here for some more examples and descriptions. http://www.excelfunctions.net/VBA-Loops.html
    Last edited by GeneralDisarray; 05-29-2013 at 08:42 AM.

  11. #11
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    OK, that worksheet is VERY close to what I need! Do you think you can change two things in the code?

    I hate to have you design the whole thing for me, but you're so close with that workbook I figure it will be easy and quick.

    1) There will be multiple lookup values in a single column which all need to be looked up individually. (yours only looks up 1 value)
    2) The results for each lookup value (if there are any) need to be thrown into the cell to its left. If it finds multiple results, they will be separated by carriage returns but still put within that single cell. (yours puts the results across multiple rows instead of in a single cell)

  12. #12
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Wink Re: A better way to store/retrieve bulk data?

    ok. Added some variables you can edit and then i think this will be exactly what you are asking for.

    You're going to have to tell the macro the following:

    comments sheet name, report (or destination) sheet name, header to look for to find the 'values' column on the report sheet, header to look for to find the 'match' column on the comments sheet.


    Run the example and I think you'll see what I'm talking about. Attached updated workbook.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    It's ALMOST working, lol. It successfully retrieves the first comment but errors out on the line "For each hit in tempMatch" with a "Runtime Error 424: Object Required" message. I did fat-finger the whole macro in so there might be a typo somewhere, but I don't see it yet. I have both tempmatch and hit qualified as ranges. Also, the "Set tempmatch..." line seems to be entered correctly.

  14. #14
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    hmm.. without seeing your code, i can't really be sure what's going on.

    Can you copy out and past what you have?

  15. #15
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    The macro itself is nearly 900 lines long. I scanned the relevant portions (the custom function at the top, and your portion of the macro at the bottom). I bracketed them off with red for you. Sorry about the scan quality. You will also have to rotate them. =( document2013-05-29-153148_2.pdf

  16. #16
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    Looks like you define HIT in the wrong place. Should be an easy fix


    Basically, you need to move that DIM statement for the "hit" range variable to the Sub that is referencing that variable.

    You have it declared in the Function findmatchrange() -- move the declaration (dim statement) for hit to whatever sub or function contains the line "For each hit in tempMatch". Notice where I have it declared in my previous example.


    You are getting an error on that line because the variable is "out of scope" -- that particular sub or function just doesn't know what to do with it. Make sense?

  17. #17
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    I'm confused. I have it declared in both the function and the Sub, just like you did. You can see both dim statements for Hit in the PDF. I tried deleting it from the Function, and only keeping it in the Sub, in case that's what you meant. No change, though.

  18. #18
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    Ok, I didn't realize I had it in both the sub and function. Just taking a guess for you, the message you posted sounds like one of the Range variables is not defined for that loop.

    Without being able to step through the code I can only guess. Can you isolate what is going wrong? Step through the code and set a watch for those range variables, where are they pointing at that line (or are they not defined at that point)?

  19. #19
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    OK, so I set a watch on "hit" and "tempmatch" and stepped through the process. It looks like multiple comments are now being put into a cell, but I'm still getting the error. Here's what happens...I've numbered the lines of the last half only of the code you gave me for reference purposes:

    Please Login or Register  to view this content.
    1) Macro loops between lines 3 and 4 a few times. The values of tempmatch and hit all look OK during this process. The value of Hit seems to change with every iteration.
    2) After doing this a few times, the script stops looping and continues on to line 6. Once it does this the Hit value is now empty.
    3) Macro reaches line 7 and loops back to line 0. When it does this, the tempmatch variable now also becomes empty.
    4) When the macro reaches line 2, it spits out the error. I assume this is because both variables are empty?

    EDIT: I ran it with different data and then it doesn't loop at all. It just errors out when it reaches line 2
    Last edited by daedelous00; 05-30-2013 at 11:27 AM.

  20. #20
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    So, it seems like it's erroring out whenever it gets to line 2 and tempMatch is empty. Sometimes it loops once before that happens, sometimes it doesn't. Trying to narrow down the issue now. Will edit this post when I find more. It may be a specific piece of data input that's doing it.

    Found the problem! If it looks for a value that isn't found, it errors out. The same thing happens in the example workbook you provided earlier.
    Last edited by daedelous00; 05-30-2013 at 11:41 AM.

  21. #21
    Registered User
    Join Date
    07-13-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: A better way to store/retrieve bulk data?

    It's working! I added the If statement
    Please Login or Register  to view this content.
    for everything after the Set tempMatch section. Thanks so much for your help!

    EDIT: Looks like it's cutting 40-50% of the time off the original latency.
    Last edited by daedelous00; 05-30-2013 at 12:14 PM.

  22. #22
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: A better way to store/retrieve bulk data?

    wow, thank you for all that.

    Well, that's to be expected. So, what do we want to happen when there is nothing to be found? Leave the cell next to the number blank and move on?

    We need to just point the thing to the next 'i' value when this happens.

    I think this would work if we insert a couple of "on error" lines to gracefully move on when this happens:

    Please Login or Register  to view this content.
    on error goto skip - this allows us to skip the loop when there is no match (note, you have to name the line above the "Next i" skip

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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