Unable to hide row Excel 2003 from function invoked from formula

前端 未结 1 645
星月不相逢
星月不相逢 2020-12-12 07:55

I have this very simple function

Public Function HRows(xx As String)
    BeginRow = 2
    EndRow = 10
   \' HideRows
    For RowCnt = BeginRow To EndRow
             


        
相关标签:
1条回答
  • 2020-12-12 08:19

    I've already introduced this method here https://stackoverflow.com/a/23232311/2165759, for your purpose a code will be as follows:

    Place code to one of the module of 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
    

    Place code to ThisWorkbook section of Microsoft Excel Objects in VBAProject:

    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
    
    0 讨论(0)
提交回复
热议问题