简体   繁体   中英

Hide and unhide specific parts of excel project

I just started with VBA and am currently struggling to find a solution to this problem:

I understand how hiding works and have code for hiding one range of rows/columns (Worksheets("Sheet3").Columns("A:G").EntireColumn.Hidden = True), but I wanted to be able to store a "list" of column + sheet and a list of rows + sheet. Then run the hiding code in a loop for every range in both lists.

At the moment this is what I've got (but I don't know how to make it work):

Sub Test()
Application.ScreenUpdating = False
Dim i As Integer
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
'To hide sheets
Sheets(Cells(i, 1).Value).Visible = False
'To hide row on sheet
Rows(Cells(i, 2).Value).Hidden = True
'To hide row on specific sheet
Sheets(Cells(i, 1).Value).Rows(Cells(i, 2).Value).Hidden = True

Next
Application.ScreenUpdating = True
End Sub

I'll just post my last response from chat here, please let me - and others! - know whether it helped.

1) Store the values which rows/columns are to be hidden on which sheet in an XML like this:

<hideme>
  <Sheets>
    <Sheet name="Sheet1">
      <rows>
        <row range="3:3"/>
        <row range="13:13"/>
      </rows>
      <columns>
        <column range="C:C"/>
        <column range="F:F"/>
      </columns>
    </Sheet>
    <Sheet name="Sheet2"/>
    <Sheet name="Sheet3">
      <rows>
        <row range="5:5"/>
      </rows>
    </Sheet>
  </Sheets>
</hideme>

For the macro, you need to set a reference to Microsoft XML, v6.0. (ie in VB Editor, Tools=>References).

Sub HideMe()

Dim xml As DOMDocument60
Dim xmlSheets As IXMLDOMNodeList
Dim n As IXMLDOMNode, r As IXMLDOMNode, c As IXMLDOMNode, xmlRows As IXMLDOMNodeList, xmlColumns As IXMLDOMNodeList
Dim ro As IXMLDOMNode, col As IXMLDOMNode, ran As String
Dim sh As Worksheet

Dim xmlpath As String

xmlpath = "[path to your xml]"

Set xml = New DOMDocument60
xml.Load (xmlpath)

Set xmlSheets = xml.DocumentElement.SelectNodes("//Sheet")

For Each n In xmlSheets 'Sheet nodes
    Set sh = ActiveWorkbook.Sheets(n.Attributes.getNamedItem("name").Text)
    Set r = n.SelectSingleNode("rows")
    If Not r Is Nothing Then
        Set xmlRows = r.SelectNodes("row")
        For i = 0 To xmlRows.Length - 1
            ran = xmlRows.Item(i).Attributes.getNamedItem("range").Text
            sh.Rows(ran).EntireRow.Hidden = True
        Next i
    End If

    Set c = n.SelectSingleNode("columns")
    If Not c Is Nothing Then
        Set xmlColumns = c.SelectNodes("column")
        For i = 0 To xmlColumns.Length - 1
            ran = xmlColumns.Item(i).Attributes.getNamedItem("range").Text
            sh.Range(ran).EntireColumn.Hidden = True
        Next i
    End If
Next n

End Sub

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