0

I have an xla with a bunch of functions. Basically each of these functions perform a vertical lookup on another worksheet. Here's a basic example of one of them:

Function ax2Project_Address(projectnumber)
If (projectnumber = "?") Then
    ax2Project_Address = "Unknown address"
Else
    ax2Project_Address = Application.VLookup(projectnumber, Sheets("AX Data").Range("A:K"), 11, False)
End Function

This allows users to simply call the function with the project number on the worksheet and it gives them the address, without having to write the vertical lookup themselves.
What I want to do now is have the row of the current cell to autofit its height to its content. The problem is that this obviously should be done after the cell has been filled with the adress, but since the function ends after the return, the autofit command won't be executed.

Is there a way to execute the autofit command in a function after the return?
Or is there a completely different approach I could use?

7
  • You can write code to Worksheet_calculate event and it'll run each time the sheet has been recalculated. Commented Jan 27, 2016 at 12:55
  • Not sure what version of Excel you are using, but have you looked at the Autofit method? Possibly add it at the end of your function or call it separately.
    – CharlieRB
    Commented Jan 27, 2016 at 13:35
  • 1
    You cannot use a worksheet function to alter anything other than the content of the cell. There's a workaround using Application.Evaluate but I couldn't get it to work with Autofit. I think the Worksheet_calculate event is your best bet.
    – Kyle
    Commented Jan 27, 2016 at 14:52
  • @Máté Using the event doesn't work, is it possible this only applies to worksheet functions, not to VBA written functions?
    – Teebs
    Commented Jan 27, 2016 at 15:17
  • @CharlieRB I'm using Excel 2013. I know about the Autofit method, but the problem is that I can't call it inside the function, because when the data gets put in the cell, the function ends, because this is the return value.
    – Teebs
    Commented Jan 27, 2016 at 15:19

2 Answers 2

0

Functions cannot change anything in excel except for the cell it's called in. That being said, I'm pretty sure a function can trigger a routine -

Option Explicit

Public Function eieio(numb As Long) As Long
    eieio = Int(numb / 2)
    Test
End Function


Sub Test()
MsgBox ("hi")
End Sub

eieio(12) = 6 and msgbox "hi"

That being said, what you can't do is something like this -

Option Explicit

Public Function eieio(numb As Long) As Long
    eieio = Int(numb / 2)
    Test
End Function


Sub Test()
Dim str As String
str = Application.Caller.Address
Dim rng As Range
Set rng = Range(str)
MsgBox (rng.Address)
rng.EntireRow.AutoFit
End Sub

So the routine can't change the application.caller as far as I know. It's always been like that.

3
  • Your second example works if instead of Test() you use the Worksheet_Calculate() event to autofit based on a global variable. Me.Cells(x,1).EntireRow.Autofit
    – Kyle
    Commented Jan 27, 2016 at 18:30
  • @Kyle yeah, I got it to work using Worksheet_Change so I could pass the target, but it's not consistent. I could just put it in worksheet_change and let it autofit everything that happens, but that's weak. We'd need an intersect method. Commented Jan 27, 2016 at 18:38
  • I think mostly everyone has given up on a work-around because conditional formatting will pretty much do what people want. Commented Jan 27, 2016 at 18:40
0

If you don't want to autofit the entire sheet use this in the Worksheet code module:

Private Sub Worksheet_Calculate()
    Dim a As Range
    For Each a In Me.UsedRange
        If a.Formula Like "=ax2Project_Address(*" Then a.EntireRow.AutoFit
    Next a

End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .