Consulting

Results 1 to 6 of 6

Thread: Functions can change some properties?

  1. #1
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location

    Functions can change some properties?

    Dear All,
    Can I write the function that :
    1. Return the value.
    2. Change some properties.

    Some things like:
    Function RowsCount(Rng As Range)
    
    On error goto ErrorHandler
    Range("A1").Font.Bold = True
    RowsCount = Rng.Rows.Count
    ErrorExit:
       Exit Function
    
    ErrorHandler:
       RowsCount = -1
       Resume ErrorExit
    End Function
    LVD

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, not if you are calling it from a worksheet, that is not possible.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    A function called from a worksheet can change the evironment if the evironmental change is done in a companion Calculate event.

    Put this in a normal module
    [VBA]Public CellsToBold As New Collection

    Function RowsCount(Rng As Range) As Long
    On Error Resume Next
    CellsToBold.Add Rng, key:=Rng.Address(, , , True)
    On Error GoTo ErrorHandler
    RowsCount = Rng.Rows.Count
    errorExit:
    On Error GoTo 0
    Exit Function
    ErrorHandler:
    RowsCount = -1
    Resume errorExit
    End Function
    [/VBA]and this in the ThisWorkbook code module[VBA]Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Dim oneCell As Range

    If 0 < CellsToBold.Count Then
    For Each oneCell In CellsToBold
    oneCell.Font.Bold = True
    Next oneCell
    Set CellsToBold = Nothing
    End If
    End Sub[/VBA]

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    mike

    I know this is possible, but is it 'really' the function thats changing the properties?

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    From the user's perspective (press this button, the screen changes that way), I'm don't think there's a difference between a function changing the environment and a Calculate event changing the environment.

    I'd split the difference and call that a work-around.

  6. #6
    VBAX Regular levanduyet's Avatar
    Joined
    Jul 2007
    Location
    HCMC
    Posts
    46
    Location
    Thanks mikerickson,

    LVD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •