0

I'm trying to make it so that my UDF function updates whenever its reference cells update.

Here's the code:

Function ConCatRange(CellBlock As Range) As String Application.Volatile True Dim cell As Range Dim sbuf As String For Each cell In CellBlock On Error GoTo fred If Len(cell.Text) > 1 Then sbuf = sbuf & cell.Text & Chr(10) Next ConCatRange = Left(sbuf, Len(sbuf) - 1) fred: End Function 

When reference cells are blank, however, it will not show #VALUE! - This is what I want. Instead, it will just not update and keep the data that was last populating the cell.

Thank you!

3
  • 2
    I assume you do not have TEXTJOIN? Commented Apr 1, 2020 at 20:59
  • In addition to @GarysStudent suggestion, Application.Volatile is unnecessary. The UDF will update whenever the referenced cells are updated; and having that there will force it to recalculate unneccesarily. Commented Apr 2, 2020 at 1:49
  • I do not have textjoin, sadly. Commented Apr 6, 2020 at 18:01

2 Answers 2

3

If you want to see #VALUE! just remove the On Error statement. This will allow the Left() function to report its failure.

Sign up to request clarification or add additional context in comments.

4 Comments

Throwing an error is different than returning an error to the cell like a built-in UDF does.
@HackSlash The end result will be the same in that #VALUE will be returned by the formula to the cell containing the formula.
This does not achieve the desired result. The function will still fail to update automatically unless I click into the cell and hit "enter" manually.
@BobbyZullo Have you set the calculation mode to Automatic ?
1

Try this:

Function ConCatRange(CellBlock As Range) As String Application.Volatile True Dim cell As Range Dim sbuf As String For Each cell In CellBlock On Error GoTo exitErr If Len(cell.Text) = 0 Then GoTo exitErr If Len(cell.Text) > 1 Then sbuf = sbuf & cell.Text & Chr(10) Next ConCatRange = Left(sbuf, Len(sbuf) - 1) Exit Function exitErr: ConCatRange = CVErr(xlErrValue) End Function 

Within the loop, you check for a Text length of 0 and if it finds one, returns CVErr(xlErrValue) - also known as "#VALUE!"

1 Comment

This does not achieve the desired result. The function will still fail to update automatically unless I click into the cell and hit "enter" manually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.