+ Reply to Thread
Results 1 to 21 of 21

vba to speed up macro

Hybrid View

  1. #1
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    vba to speed up macro

    Hello,

    I have got a worksheet with 18 columns which I need to update from a different using the index and match formula in excel. The problem is that I use data from SAP which seems to be extremely difficult to be read by excel. During the calculations below on the sheet I can see something like Calculating 2processor(s)) --%" and it takes about 20min for excel to update all 18 columns with 10 000 rows. Is there any way I can use vba (for example) arrays to speed up the whole process? The actual formula which I use is the following:

    =IFERROR(INDEX('SAP 5 Download'!$C$7:$C$117,MATCH('SAP 5 Tracker'!D8&'SAP 5 Tracker'!E8,'SAP 5 Download'!$A$7:$A$7912&'SAP 5 Download'!$B$7:$B$7911,0)),"")
    The thread is open here too
    http://www.mrexcel.com/forum/excel-q...ion-macro.html

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: vba to speed up macro

    Hi
    Could you attach a sample of your file

  3. #3
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Hello Nilem,

    I would attach it but the workbook is just too large. When I put the data from SAP to Excel the workbook's size suddenly changes to about 15-20 mb although there are only 10 sheets in. And that's why it takes so long for the calculations to take place. I guess this is a SAP-Excel issue. I am not sure. I was just wondering whether I could you use the evaluate function in vba to speed up the calculations. For example from A7 till A10000 I want to use the formula above whereas the lookup value changes as we go down the rows. In other words I want to make the calculations out of the sheet (as an array in vba). Is that possible?

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    @dulitul,

    Would it be possible for you to describe in plain text what you are trying to achieve with the formula's. In other words what are you trying to retrieve from the tracker worksheet?
    If you like my contribution click the star icon!

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: vba to speed up macro

    or you could make a small sample of your file, leaving a few rows in each sheet in order to calculation logic would be understandable

  6. #6
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Hello Ollie,

    The description is as follows:

    in sheet 'SAP 5 Download' I have got three columns where I paste the data from SAP

    col. A , B , C

    Then I want to update column C in sheet 'SAP 5 Tracker' with column C from 'SAP 5 Download' if there is any match between columns A-B from 'SAP 5 Download' with columns D-E from 'SAP 5 Tracker' then the data from column C in 'SAP 5 Download' should be transferred in column C in 'SAP 5 Tracker'.

    Example : A5-B5 in 'SAP 5 Download' equal D55-E55 in 'SAP 5 Tracker' then the value from C5 'SAP 5 Download' is pasted into C55 in 'SAP 5 Tracker' . And this is what I have to do for 18 columns with 10 000 rows each. That's why I use the index and match foruma. I hope it's now clear.
    Last edited by dulitul; 09-24-2013 at 05:38 AM.

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    Would you be OK with a VBA solution that you call after the paste of data from SAP?

  8. #8
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Hello,

    Here I have simulated a tiny part of my sheets just principally to show you the worksheet formula. As I said as the data is complex it takes ages until all calculations are done so I will be happy to do that using vba and to be able to cut down the time it takes.

    Thank you
    Attached Files Attached Files

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    Noticed in your test file that the download sheet holds columns C, D and E, and the tracker worksheet A, B and C. From your earlier description I thought it was the other way around. My code assumes columns ABC in the download worksheet and DEF in the tracker worksheet. Please advise what is correct?

  10. #10
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    Please try the following code

    Public Sub updateTrackerWorksheet()
    
    '#
    '# declare private variables
    '#
       Dim pvt_obj_Tracker As Excel.Worksheet
       Dim pvt_obj_Index As Object
       Dim pvt_lng_RowNumber As Long
       Dim pvt_str_KeyValue As String
       
    '#
    '# create a dictionary object to act as an index for all rows on the tracker
    '# worksheet
    '#
       Set pvt_obj_Tracker = ThisWorkbook.Worksheets("SAP Tracker")
       Set pvt_obj_Index = CreateObject("Scripting.Dictionary")
       
       With pvt_obj_Tracker
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "D").End(xlUp).Row
             pvt_str_KeyValue = .Cells(pvt_lng_RowNumber, "D").Value & "|" & .Cells(pvt_lng_RowNumber, "E").Value
             If Not pvt_obj_Index.Exists(pvt_str_KeyValue) Then
                pvt_obj_Index.Add pvt_str_KeyValue, pvt_lng_RowNumber
             End If
          Next pvt_lng_RowNumber
       End With
       
    '#
    '# process all rows from the SAP download worksheet and for each key (combined column A and B) determine
    '# if a matching row exists on the Tracker worksheet - if found updated column C on the tracker worksheet
    '# with the value of column C on the download worksheet
    '#
       With ThisWorkbook.Worksheets("SAP Download")
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "D").End(xlUp).Row
             pvt_str_KeyValue = .Cells(pvt_lng_RowNumber, "A").Value & "|" & .Cells(pvt_lng_RowNumber, "B").Value
             If pvt_obj_Index.Exists(pvt_str_KeyValue) Then
                pvt_obj_Tracker.Cells(pvt_obj_Index(pvt_str_KeyValue), "C").Value = .Cells(pvt_lng_RowNumber, "C").Value
             End If
          Next pvt_lng_RowNumber
       End With
       
    '#
    '# housekeeping
    '#
       Set pvt_obj_Index = Nothing
    
    End Sub
    1. correct the names of the worksheets in the code
    2. remove all formulas from your workbook
    3. run the code after pasting the data in the download worksheet

  11. #11
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Sorry it is exactly as on the sheet I have pasted. Ignore my comment before. I have copied and pasted the code without changing anything directly into the workbook test but it doesn't seem to do anything? Can you please double check?

  12. #12
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Also, I was wondering whether we could update 9 other columns not just column C. For example columns F through N ?

  13. #13
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    OllieB I have just tested the macro on the same sheet and it does not work.

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    Quote Originally Posted by dulitul View Post
    OllieB I have just tested the macro on the same sheet and it does not work.
    I have provided revised code. Your original description of the requirement and the test workbook provided later did not match, hence the code obviously did not work either. The new code has been tested against the example workbook and no problems were encountered

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

    Re: vba to speed up macro

    "The problem is that I use data from SAP which seems to be extremely difficult to be read by excel."
    I had experienced this issue a few times in the past. My PC had crashed 3 times. Data exported from external application to tend to contain lots of unseen dirty data. Even if macro will not help lots. The simples solution is to copy values ONLY in to new sheet. Do not leave or work on data imported. You should first convert the data in to values only and delete the imported data. You will see a massive difference.

  16. #16
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: vba to speed up macro

    Another thing to think about is if you have access to MS Access, it may be easier to set up tables and queries there. When you are doing multiple selections, Access has the capability to set up those kind of relationships.

  17. #17
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Oh well. I have pasted the data in another workbook using ctrl + V and reinputted the formula but it seems to be slow again. Don't know what to do.

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

    Re: vba to speed up macro

    ctrl + V is copy and paste. You need to highlight the entire sheet- click top right hand(Above A1).copy and then paste values ONLY, not just enter which tends to copy and paste all.

  19. #19
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    @dulitul,

    I have adjusted the code to match the example workbook provided by you.

    Public Sub updateTrackerWorksheet()
    
    '#
    '# declare private variables
    '#
       Dim pvt_obj_Tracker As Excel.Worksheet
       Dim pvt_obj_Index As Object
       Dim pvt_lng_RowNumber As Long
       Dim pvt_str_KeyValue As String
       
    '#
    '# create a dictionary object to act as an index for all rows on the tracker
    '# worksheet
    '#
       Set pvt_obj_Tracker = ThisWorkbook.Worksheets("SAP Tracker")
       Set pvt_obj_Index = CreateObject("Scripting.Dictionary")
       
       With pvt_obj_Tracker
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
             pvt_str_KeyValue = .Cells(pvt_lng_RowNumber, "A").Value & "|" & .Cells(pvt_lng_RowNumber, "B").Value
             If Not pvt_obj_Index.Exists(pvt_str_KeyValue) Then
                pvt_obj_Index.Add pvt_str_KeyValue, pvt_lng_RowNumber
             End If
          Next pvt_lng_RowNumber
       End With
       
    '#
    '# process all rows from the SAP download worksheet and for each key (combined column A and B) determine
    '# if a matching row exists on the Tracker worksheet - if found updated column C on the tracker worksheet
    '# with the value of column C on the download worksheet
    '#
       With ThisWorkbook.Worksheets("SAP Download")
          For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "D").End(xlUp).Row
             pvt_str_KeyValue = .Cells(pvt_lng_RowNumber, "D").Value & "|" & .Cells(pvt_lng_RowNumber, "E").Value
             If pvt_obj_Index.Exists(pvt_str_KeyValue) Then
                pvt_obj_Tracker.Cells(pvt_obj_Index(pvt_str_KeyValue), "C").Value = .Cells(pvt_lng_RowNumber, "C").Value
             End If
          Next pvt_lng_RowNumber
       End With
       
    '#
    '# housekeeping
    '#
       Set pvt_obj_Index = Nothing
    
    End Sub
    If you want to copy more columns please advise on which columns to copy and to which target location

  20. #20
    Registered User
    Join Date
    12-14-2012
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: vba to speed up macro

    Brilliant OllieB. The macro works fantastic!! I have updated it. Thank you.

  21. #21
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: vba to speed up macro

    @dulitul,

    You are welcome. May I please ask you to mark the thread as closed as per the forum rules and if you are happy with my contribution to click on the star icon. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Speed up a macro...
    By kokapelly in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-10-2013, 02:17 PM
  2. [SOLVED] How To Speed up macro
    By AnkitGuru in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-02-2012, 06:36 AM
  3. Speed up macro
    By Jasrenkai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2010, 10:07 AM
  4. [SOLVED] Speed up a macro
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 02-17-2010, 05:05 PM
  5. [SOLVED] Macro Speed
    By Don Lloyd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2005, 02:06 PM

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