Excel VBA - 获取工作表的父窗口

时间:2013-12-19 00:31:54

标签: excel vba excel-vba

我有一个Sub,它从模板中插入一个标题并冻结活动工作表的顶行,写成,

Sub HeaderInsert(headerTemplate As Worksheet)
    headerTemplate.Rows("1:1").Copy
    ActiveSheet.Rows("1:1").Select
    ActiveSheet.Paste
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
        .FreezePanes = True
    End With
End Sub

我想把它变成一个传递工作表以插入标题的函数。所以它会被写出来,

Function HeaderInsert(headerTemplate As Worksheet, contentSheet as Worksheet)

ActiveSheet变为contentSheet,但如何获得Window的{​​{1}}?

还有更好的方法来复制和粘贴吗?

1 个答案:

答案 0 :(得分:11)

我认为你想要contentSheet.Parent.Windows(1),例如:

Sub test()
Dim ws As Excel.Worksheet
Dim wb As Excel.Workbook

Set ws = ActiveSheet
Set wb = ws.Parent
Debug.Print wb.Windows(1).Caption
End Sub

更好的粘贴方式: headerTemplate.Rows(“1:1”)。复制ActiveSheet.Rows(“1:1”)

更一般地说,您想要avoid Select unless necessary.