Results 1 to 16 of 16

Event Sub from run Function Event

Threaded View

vbalearnerSF Event Sub from run Function... 08-05-2014, 03:21 PM
mikerickson Re: Event Sub from run... 08-05-2014, 03:29 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 03:37 PM
Norie Re: Event Sub from run... 08-05-2014, 03:41 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 03:53 PM
Norie Re: Event Sub from run... 08-05-2014, 03:55 PM
MrShorty Re: Event Sub from run... 08-05-2014, 03:57 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 04:04 PM
Norie Re: Event Sub from run... 08-05-2014, 04:09 PM
mikerickson Re: Event Sub from run... 08-05-2014, 04:14 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 04:21 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 04:23 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 04:56 PM
Norie Re: Event Sub from run... 08-05-2014, 04:47 PM
vbalearnerSF Re: Event Sub from run... 08-05-2014, 04:54 PM
mikerickson Re: Event Sub from run... 08-05-2014, 10:24 PM
  1. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Event Sub from run Function Event

    The way that I would do it is

    'in the UDF
    test Application.Caller against the result array,
    if it (the range Application.Caller) is the right size, return the array: DONE
    if it is not
    pass the current range (to be blanked)
    pass the range of the correct size
    pass the formula
    End UDF

    ' in the Calculate event
    Blank the current (wrong sized range)
    put the formula in the correct sized range. This triggers the UDF again, which will go out the OK branch.

    Here is an example. Note that this heavily assumes that the result is wanted in a column.
    put =myArrayFtn(A1) in a cell (not in column A) and change the value of A1.
    Then put =myArrayFtn(B1) in a different cell.

    ' in a normal module
    
    Function myArrayFtn(N As Long) As Variant
        Dim i As Long
        Dim arrResult As Variant
        Dim keyAddress As String
        Dim resultSize As Long
        
        Rem get values for array
        ReDim arrResult(1 To N)
        For i = 1 To N
            arrResult(i) = i
        Next i
        
        Rem handle displaying the results
        resultSize = UBound(arrResult)
    
        If TypeName(Application.Caller) = "Range" Then
    
            If Application.Caller.Rows.Count <> resultSize Then
                Rem if array formula is wrong size, send info to collections for adjustment
    
                keyAddress = Application.Caller.Resize(resultSize, 1).Address(, , , True)
                
                With ThisWorkbook
                    .CellsForFormula.Add Item:=Application.Caller.Resize(resultSize, 1), Key:=keyAddress
                    .FormulaForCells.Add Item:=Application.Caller.FormulaArray, Key:=keyAddress
                    .CellsToBlank.Add Item:=Application.Caller, Key:=keyAddress
                End With
                
                myArrayFtn = False
            Else
                Rem if called by array formula (of the correct size), transpose for column output
                myArrayFtn = Application.Transpose(arrResult)
            End If
        Else
            Rem if not called by worksheet formula
            myArrayFtn = arrResult
        End If
    
    End Function
    ' in ThisWorkbook code module
    
    Public CellsForFormula As New Collection
    Public FormulaForCells As New Collection
    Public CellsToBlank As New Collection
    
    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        Dim keyAddress As String
        Dim oneRange As Range
        For Each oneRange In Me.CellsForFormula
            keyAddress = oneRange.Address(, , , True)
            Me.CellsToBlank(keyAddress).ClearContents
            Me.CellsForFormula(keyAddress).FormulaArray = Me.FormulaForCells(keyAddress)
        Next oneRange
        With Me
            Set .CellsForFormula = Nothing
            Set .CellsToBlank = Nothing
            Set .FormulaForCells = Nothing
        End With
    End Sub
    Last edited by mikerickson; 08-06-2014 at 01:28 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Userform multipage control - exit event not firing or event order
    By jane serky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2013, 10:23 AM
  2. Deactivate event appears to be overriding next activate event
    By ezrizer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-01-2013, 10:15 AM
  3. Replies: 4
    Last Post: 11-07-2012, 04:02 PM
  4. How to prevent SelectionChange event firing before Change event?
    By franklyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2012, 05:17 AM
  5. MsgBox in Enter event causes combobox not to run Change event
    By Richard in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2006, 10:55 AM

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