+ Reply to Thread
Results 1 to 5 of 5

VBA performance with building formulas

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    154

    VBA performance with building formulas

    I am having a performance issue with this code.

        
    
    Application.Calculation = xlCalculationManual
    mypath = "'L:\xx\"
    p = "[xxx.xls]"
    x = mypath & p
    
        lcolumn = Range("B3").End(xlToRight).Column
    
        For Each rCell In Range(Cells(4, 3), Cells(4, lcolumn))
            
            y = Cells(rCell.Row, 2).Address(0, 1)
            z = Cells(1, 2).Value & Cells(2, rCell.Column).Value
            q = Cells(1, rCell.Column).Address(1, 0)
            
    
            rCell.Formula = "=IF(" & y & "=" & Chr(34) & "Placeholder" & Chr(34) & ",0,vlookup(" & y & "," & x & z & "'!$C$12:$P$2000," & q & ",FALSE))"
        Next rCell
    The above code takes forever to put the formula into each cell. However, if I change the code for rcell to "rcell.value=1", it flies through it. Is there a way to make the formula more efficient?

    Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: VBA performance with building formulas

    Not sure, but what if you change rCell.Formula to rCell.Value?

  3. #3
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    154

    Re: VBA performance with building formulas

    thanks for suggestion..did not make it any faster

  4. #4
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: VBA performance with building formulas

    Can you post the full macro? Maybe it would give additional insight.

  5. #5
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    154

    Re: VBA performance with building formulas

    That really is the full macro. See below

    Option Explicit
    
    Sub consolidation()
    
    Dim mypath As String
    Dim rCell As Range
    Dim x As String
    Dim y As String
    Dim z As String
    Dim q As String
    Dim p As String
    
    Dim lcolumn As Integer
    
    Application.Calculation = xlCalculationManual
    
    mypath = "'L:\xx\"
    p = "[xx.xls]"
    x = mypath & p
    
    
        lcolumn = Range("B3").End(xlToRight).Column
    
        For Each rCell In Range(Cells(4, 3), Cells(4, lcolumn))
            
            y = Cells(rCell.Row, 2).Address(0, 1)
            z = Cells(1, 2).Value & Cells(2, rCell.Column).Value
            q = Cells(1, rCell.Column).Address(1, 0)
            
    
            
            
            rCell.Formula = "=IF(" & y & "=" & Chr(34) & "Placeholder" & Chr(34) & ",0,vlookup(" & y & "," & x & z & "'!$C$12:$P$2000," & q & ",FALSE))"
        Next rCell
        
        
    Application.Calculation = xlCalculationAutomatic
    Application.Calculate
    Application.ScreenUpdating = True
    
    End Sub
    I did some tests to confirm that the slow part is putting the formula into "rCell"

+ 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