简体   繁体   中英

VBA function not hidding columns when used in an excel cell

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?

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM