【问题标题】:Unable to hide row Excel 2003 from function invoked from formula无法从公式调用的函数中隐藏行 Excel 2003
【发布时间】:2014-08-04 21:36:18
【问题描述】:

我有这个非常简单的功能

Public Function HRows(xx As String)
    BeginRow = 2
    EndRow = 10
   ' HideRows
    For RowCnt = BeginRow To EndRow
     Cells(RowCnt,ChkCol).EntireRow.Hidden = True
    Next RowCnt
End Function 

当从命令按钮调用时,它工作正常,当作为公式调用时,例如 =HRows(A1),从工作表单元格调用它在 Excel 2003 上没有任何作用,它在 Open Office Calc 4.1 中工作

这发生在一个空的电子表格上 - 没有保护、没有 cmets、没有形状(在其他问题中被建议作为抑制因素)

最终,我想隐藏/显示电子表格的相关部分,具体取决于用户在某些关键单元格中输入的内容 - 我不想添加命令按钮来控制隐藏。

【问题讨论】:

  • 通常不能使用 UDF 更改工作表的状态。然而,蒂姆·威廉姆斯 (Tim Williams) 不久前发布了这个 (stackoverflow.com/q/23433096/2119523) 非常出色的解决方法。我不确定这是否适用于隐藏行。

标签: excel excel-formula hide rows vba


【解决方案1】:

我已经在这里https://stackoverflow.com/a/23232311/2165759介绍了这个方法,为了你的目的,代码如下:

将代码放置到 VBAProject 的模块之一:

Public Tasks, PermitNewTasks, ReturnValue

Function HideRowsUDF(lBegRow, lEndRow) ' Use this UDF on the sheet
    If IsEmpty(Tasks) Then TasksInit
    If PermitNewTasks Then Tasks.Add Application.Caller, Array(lBegRow, lEndRow)
    HideRowsUDF = ReturnValue
End Function

Function HideRows(lFrom, lUpTo) ' actually all actions performed within this function, it runs without UDF limitations
    Range(Rows(lFrom), Rows(lUpTo)).EntireRow.Hidden = True
    HideRows = "Rows " & lFrom & "-" & lUpTo & " were hidden"
End Function

Sub TasksInit()
    Set Tasks = CreateObject("Scripting.Dictionary")
    ReturnValue = ""
    PermitNewTasks = True
End Sub

将代码放置到 VBAProject 中 Microsoft Excel 对象的 ThisWorkbook 部分:

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Dim Task, TempFormula
    If IsEmpty(Tasks) Then TasksInit
    Application.EnableEvents = False
    PermitNewTasks = False
    For Each Task In Tasks
        TempFormula = Task.FormulaR1C1
        ReturnValue = HideRows(Tasks(Task)(0), Tasks(Task)(1))
        Task.FormulaR1C1 = TempFormula
        Tasks.Remove Task
    Next
    Application.EnableEvents = True
    ReturnValue = ""
    PermitNewTasks = True
End Sub

【讨论】:

  • 谢谢你 - 答案显然比我希望的要复杂。我需要解决更紧迫的问题才能回来解决这个问题
猜你喜欢
  • 2013-11-27
  • 2016-10-16
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多