0

I am trying to use the functions below to hide columns

Function SCPArgsShowOnly() As Boolean
    Dim sColsToHide As String
    sColsToHide = "E:I,M:N"
    hideCols sColsToHide
    SCPArgsShowOnly = True
End Function

'======================================================
Sub hideCols(sCols As String)
    Dim sTemp() As String, allCols As String
    sTemp = Split(sCols, ",")
    allCols = "A:N"

    With Sheets("Functions")
        .Columns(allCols).Hidden = False

        For i = LBound(sTemp) To UBound(sTemp)
            .Columns(sTemp(i)).Hidden = True
        Next
    End With
End Sub

It works fine when I run it through the debugger window. But it only returns true when I use it in a cell like this = SCPArgsShowOnly()

What am I missing?

5
  • 4
    you can't use a UDF to hide columns Commented Jan 31, 2017 at 7:41
  • how do you call it if not from debugger window? do you have an autostart macro for your excel file? Commented Jan 31, 2017 at 7:44
  • How do you call SCPArgsShowOnly() ? It does return true every time. Commented Jan 31, 2017 at 7:50
  • 1
    Shai is right it can't be done with a UDF embedded in the worksheet - but maybe when it's enabled by a button? Commented Jan 31, 2017 at 8:08
  • 1
    As Shai says a standard UDF can't interact with the sheet. But here's a workaround Commented Jan 31, 2017 at 8:27

1 Answer 1

2

Excel does not allow calling functions that DO something from a cell.
Only functions that just return a value can be used, and that seems quite logical.
Imagine your columns appearing or disappearing whenever an entry in the sheet triggers a recalc ?

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.