Results 1 to 15 of 15

Using VBA to populate cells with formulas containing user-defined functions

Threaded View

  1. #1
    Registered User
    Join Date
    04-12-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    67

    Using VBA to populate cells with formulas containing user-defined functions

    Hi,

    As per title, I'm trying to populate some formulas from VBA but having problems. This is the UDF and populating sub:

    Function GetDateIfValid(DateCell As Range)
    On Error GoTo CleanExit
    If IsDate(DateCell.Value) And DateCell.Value <= Date And DateCell.Offset(-1, 1) <> "D" Then
        GetDateIfValid = DateCell.Value
        Exit Function
    End If
    
    CleanExit:
    GetDateIfValid = ""
    Application.Calculate
    End Function
    
    Sub populateDatesFormulae()
    Dim TC As String
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    For Each cell In Range("A2:A60")
        TC = Cells(2, (cell.Row - 1) * 3).Address
        cell.Formula = "=GetDateIfValid(INDIRECT(""'Employee Tracking'!" & TC & """))"
    Next cell
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    When I run it I just get the hourglass until I hit escape. It puts the formula into the first cell correctly but it has a '#VALUE!' error and does not continue with the other cells. If I then hit 'calculate' the formula works fine.

    edit - I've now found that if I leave the calculation as automatic it will populate all the cells but still hangs afterwards.

    Been messing around for a while with this so any suggestions would be gratefully received.

    Thanks,

    Chinchin
    Last edited by Chinchin; 08-28-2011 at 01:56 PM.

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