+ Reply to Thread
Results 1 to 11 of 11

Macro to get values and consolidate them.

  1. #1
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Macro to get values and consolidate them.

    Hey Guys,

    I have a macro and it's currently taking several minuites to run when I know it should be able to made quicker but I have no idea how to do it.

    Here is a copy of the old macros:

    Please Login or Register  to view this content.
    The object of the code is to find all numbers in BAQ and put them into BAS but compress them to get rid of all of the blank spaces.

    Could anyone improve this for me?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to get values and consolidate them.

    Difficult to comment without seeing the request in context, but you may find that a better solution is to filter the data and do the copying and deleting en bloc.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Macro to get values and consolidate them.

    I've added a small example.

    It doesn't show how long it takes in this example which is a bit annoying. My original sheet has over 50,000 rows used and is up to column CDL.
    Whenever we use Filter it takes 3-5 minuites to update so Fliter is NOT a good option for me.

    Thanks in advance.
    - Hyflex
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    12-01-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    303

    Re: Macro to get values and consolidate them.

    Interested in the outcome of this one, could use it myself. Bump.

  5. #5
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Macro to get values and consolidate them.

    Quote Originally Posted by ad9051 View Post
    Interested in the outcome of this one, could use it myself. Bump.
    I would guess the best way to do it would be to have some sort of loop searching for xlNumber in column C and to put the value into E6 then when it finds the next value it would look for the endrow + 1

    It would work much faster than deleting cells

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to get values and consolidate them.

    Whenever you have this many rows try and avoid loops like the plague. In fact try and avoid loops full stop if possible. They are always without exception very slow, particularly when each iteration through the loop has to jump backwards and forward between VBA and the Excel App.

    Always try and use bog standard Excel functionality since this will always be more efficient. Even if you need to encode it in a macro. In cases like this the fastest way I know is to use a Data Filter to highlight the data and then do with the filtered data whatever you want. If you have many formulae and filtering is slow then first switch to manual calc., switrching back to automatic at the end.

    Try the following which is the equivalent of your looping macro

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Macro to get values and consolidate them.

    That makes a mess of the sheet, I don;t understand what that macro is doing and also AutoFilters seem to take forever to run because of the sheer amount of data in the sheet but i#ll give one a go

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to get values and consolidate them.

    Sorry , I forgot to add the workbook (and mention that I've added a working sheet). It's now attached.

    The macro is quite simple. It first filters your column C to identify the non blank cells. It then copies these to the 'Working' sheet until the filter is removed, and then they're copied back to the original sheet.

    Are you running it against your small sample of data for which it was written? You'll need to adapt it for your actual data.

    Have you switched off automatic calculation? Believe me you should not be using a loop to process this data. Data Filter is the way to go, but given the 50000 rows you'll be better advised to switch off calculation since filtering otherwise causes recalculations to take place.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to get values and consolidate them.

    Richard,

    I may be breaking the rules of the forum but I could not resist commenting on this thread. I am newbie to VBA and very keen to understand how the filter in VBA works.

    Several people commented before that Filtering is much faster than looping. Does the filter in VBA works the same as Filter in excel? If it is, does it not filer sometimes messed up formulas and rows? For e.g filter in Table is not always reliable. I know this from personal experience. Or filer in VBA works differently as the automatic calculation is off. If this is the case, would it be easier to incorporate the calc manual in the VBA as you did, instead of going to excel option to change it?

    Your feedback would be much appreciated.

  10. #10
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to get values and consolidate them.

    @AB33

    I don't think you're breaking the rules since this is a continuation of the subject of the thread not a new question.

    Yes, as far as I'm aware and having used Data Filtering for more years than I care to remember, invoking Data Filtering from VBA is just as if you run it directly from Excel. Which is pretty much what you'd expect.

    I have seen mention of Tables (introduced in 2007 onwards) sometimes not filtering correctly. At this site http://forums.techguy.org/business-a...-problems.html this very same problem has been discussed. If you look at post #11 there is an attachment which does demonstrate an anomaly. Filtering any of the columns always includes the last row 94 of the table. A suggested 'solution' in post #12 is to convert the table to a range. When I tried that though it made no difference. However when I copied the table and pasted it to a blank sheet as a range it filtered correctly.

    Not sure where that leaves us since it does seem there is a bug. Perhaps someone else (an MVP perhaps) will chip in and comment. I tend not to use tables in preference to a normal range mostly because formula referencing is not as intuitive, and unless you have a particular need to use the gee whiz formatting stuff it may be better to avoid them.

    As a general rule if I have hundreds of rows which need manipulating with a filter or updating with formulae, and I know it's not necessary for calculations to take place during the macro, (e.g. when any formula that are added are not going to be converted to values before the macro finishes), I generally switch off automatic calculation until the macro has finished.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to get values and consolidate them.

    Richard,
    Thanks again! I have will a look at the linked sites. In the mean time, It would be greate if others could contribute thier experience.

+ 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