excel vba freeze pane without select

后端 未结 5 1349
情歌与酒
情歌与酒 2020-11-30 07:39

I have a VBA script in Excel that freezes the panes of an Excel worksheet, but I\'m curious to see if this is possible without first selecting a range. Here\'s by code now

5条回答
  •  一个人的身影
    2020-11-30 08:34

    I found the previous answers only worked with some sheets when looping through tabs. I found the following code worked on every tab I looped through (target was a single workbook), despite which workbook was the activeworkbook.

    The short of it:

    With Application.Windows(DataWKB.Name) 
        Application.Goto ws.Cells(4, 5)
        .SplitColumn = 4
        .SplitRow = 3
        .FreezePanes = True
    End With
    

    The code as it is in my Sub: (be aware, I do a lot more formatting in this sub, I tried to strip that out and leave just the code needed here)

    Sub Format_Final_Report()
    Dim DataWKB As Workbook
    Set DataWKB = Workbooks("Report.xlsx")
    Dim ws As Worksheet
    
    Dim tabCNT As Long
    Dim tabName As String
    tabCNT = DataWKB.Sheets.Count
    
    For i = 1 To tabCNT
        Set ws = DataWKB.Worksheets(i)
        tabName = ws.Name
    
    
        With Application.Windows(DataWKB.Name)
            Application.Goto ws.Cells(4, 5)
            .SplitColumn = 4
            .SplitRow = 3
            .FreezePanes = True
        End With
    
    Next i
    
    End Sub
    

    Hopefully, this will save someone some research time in the future.

提交回复
热议问题