如何使用VBA

时间:2015-05-11 16:06:37

标签: vb.net excel

我想在Excel中为不同的工作表设置不同的冻结窗格。我尝试使用以下代码,但这被分配给所有工作表。但我想在我的第一张纸上冻结前6行,在第二张纸上只冻结前排。我尝试了很多方法但没有结果。有什么建议?

With xlApp.ActiveWindow
    .SplitColumn = 0
    .SplitRow = 6
End With

完整代码(来自评论)是:

Dim xlApp As Application 
Dim xlWb As Workbook 
'Dim xlWs As Worksheet 
xlApp = New Application() 
xlWb = xlApp.Workbooks.Open(csFileName) 
With xlApp.ActiveWindow 
  .SplitColumn = 0 
  .SplitRow = 6 
End With

1 个答案:

答案 0 :(得分:0)

尝试这一点,替换工作表名称,行和&具有适当值的列:

Worksheets("Sheet1").activate
ActiveWindow.SplitColumn = 0
Activewindow.SplitRow = 6

Worksheets("Sheet2").activate
ActiveWindow.SplitColumn = 5
Activewindow.SplitRow = 11

Worksheets("Sheet3").activate
ActiveWindow.SplitColumn = 10
Activewindow.SplitRow = 16

根据您在评论中发布的代码,这应该有效:

Dim xlApp As Application 
Dim xlWb As Workbook 
xlApp = New Application() 
xlWb = xlApp.Workbooks.Open(csFileName) 
xlwb.worksheets("Sheet1").activate
With xlApp.ActiveWindow 
  .SplitColumn = 0 
  .SplitRow = 6 
End With
xlwb.worksheets("Sheet2").activate
With xlApp.ActiveWindow 
  .SplitColumn = 0 
  .SplitRow = 1
End With

我建议始终明确指定您希望为此启用哪个工作表,因为它需要ActiveWindow才能运行,这样您就不会意外地在当您设置分割行时,窗口错误。

Dim xlApp As Application 
Dim xlWb As Workbook 
xlApp = New Application() 
xlWb = xlApp.Workbooks.Open(csFileName) 
xlwb.worksheets("Sheet1").activate
xlWb.worksheets("Sheet1").range("A6").select
xlApp.ActiveWindow.FreezePanes
xlwb.worksheets("Sheet2").activate
xlWb.worksheets("Sheet2").range("A2").select
xlApp.ActiveWindow.FreezePanes