如何使用Excel上的VBA宏在不同的工作表上应用不同的FreezePanes

时间:2018-06-05 04:38:13

标签: excel vba excel-vba

我目前有一个包含多个工作表的工作簿,我正在尝试在我的大多数不同工作表上应用FreezePanes功能。然而,我的一张纸我想对它应用不同的规则,所以它只冻结前2列而不是像所有其他纸张一样冻结7列。

我的“Pivots”标签是我想要应用单独规则的标签。

我的代码atm是:

  Sub FreezePanes()

  sheetlist = Array("1", "2", "3", "4", "5", "6", "7", "8")
  'this is used so that all sheets freeze

For i = LBound(sheetlist) To UBound(sheetlist)
     Worksheets(sheetlist(i)).Activate
     ' The above is the loop that will go through all the different items in the array to direct it at each worksheet

     Columns("E:E").Columns.Group
     Columns("H:N").Columns.Group 'to change
     Columns("R:S").Columns.Group 'to change

     Columns("H:H").Select
     ActiveWindow.FreezePanes = True
    'code to freeze column H on those particular items

    Range("A7").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.AutoFilter
    'code to apply filter on 

ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1

  Next

 Worksheets("Pivots").Activate
 Columns("A:A").Select
 ActiveWindow.FreezePanes = True
'code to apply freeze panes only on first two columns
End Sub

我还有以下代码可以帮助拆分它但看起来并不整洁。

  Sub test()

  Worksheets("Pivots").Activate
  ActiveWindow.SplitColumn = 2
  ActiveWindow.SplitRow = 0

  End Sub

1 个答案:

答案 0 :(得分:1)

我设法找出问题所在。

现在好了。

 Worksheets("Pivots").Activate
 Columns("C:C").Select
 ActiveWindow.FreezePanes = True
'code to apply freeze panes only on first two columns
 End Sub