简体   繁体   English

在Excel单元格中使用时,VBA函数不会隐藏列

[英]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() 但是只有在像这样的单元格中使用它时,它才返回true = SCPArgsShowOnly()

What am I missing? 我想念什么?

Excel does not allow calling functions that DO something from a cell. Excel不允许调用执行单元格操作的函数。
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 ? 想象一下,每当工作表中的条目触发重新计算时,您的列出现还是消失?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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