Microsoft Excel
--
Questions
--
Followers
Top Experts
For reference (this is not the exact code; I'd have to sanitize it first to post, but I'm hopefully providing enough for your reference for now):
Worksheet = 2
IndexNumberCol = 2
Set ExcelObject = CreateObject("Excel.Applic
Set WorkbookObject = ExcelObject.Workbooks.Add(
Set WorksheetObject = ExcelObject.ActiveWorkbook
'This all works below
      WorksheetObject.Cells(1,1)
      WorksheetObject.Cells(1,1)
      WorksheetObject.Range(Work
      WorkSheetObject.Cells(1,1)
      WorkSheetObject.Cells(1,1)
      WorkSheetObject.Cells(1,1)
    FreezePanes(2)
'This is choking - usually on the Select...
Sub FreezePanes(SheetNumber)
      'WorksheetObject.Range(Wor
      ExcelObject.ActiveWorkbook
      ExcelObject.ActiveWorkbook
      ExcelObject.ActiveWorkbook
End Sub
I want to reiterate, there's a LOT more code than this, but I don't have time to sanitize it to post. Â If something jumps out, please let me know. Â Thanks.
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Sub FreezePanes(SheetNumber)
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Range("A" & Trim(CStr(Row + 1))).Select
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Parent.Windows(1).FreezePanes = True
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Range("A1").Select
End Sub
Also what kind of pane freeze do you want? Top rows or custom panes?
E.g.VBA
Option Explicit
Public Sub test()
ActiveWorkbook.Sheets("Sheet3").Select
FreezePanes ActiveWorkbook.Sheets("Sheet1")
FreezePanes ActiveWorkbook.Sheets("Sheet2"), "C3"
End Sub
Public Sub FreezePanes(ByVal CWorksheet As Excel.Worksheet, Optional ByVal CSplitAddress As String = "")
CWorksheet.Select
If CSplitAddress = "" Then
' Top Row
CWorksheet.Parent.Windows(1).SplitColumn = 0
CWorksheet.Parent.Windows(1).SplitRow = 1
Else
' Custom
CWorksheet.Range(CSplitAddress).Select
End If
CWorksheet.Parent.Windows(1).FreezePanes = True
CWorksheet.Range("A1").Select
End Sub
As VBScript:Sub FreezePanes(CWorksheet, CSplitAddress)
CWorksheet.Select
If CSplitAddress = "" Then
' Top Row
CWorksheet.Parent.Windows(1).SplitColumn = 0
CWorksheet.Parent.Windows(1).SplitRow = 1
Else
' Custom
CWorksheet.Range(CSplitAddress).Select
End If
CWorksheet.Parent.Windows(1).FreezePanes = True
CWorksheet.Range("A1").Select
End Sub
And for your code:
Set WorksheetObject = ExcelObject.ActiveWorkbook.Worksheets(Worksheet) 'There are 3 worksheets created
The number of sheets created is an Excel setting. This is not a constant. Thus don't assume that every users has this set to 3. Especially since Excel 2016 you'll find often 1 as corporate default.
To answer Ste5an's questions:
Row is a variable that starts as a defined integer and is used in various loops. so that excel rows increase appropriately as the data is populated.
The frozen panes would be lines below the Row variable and to the columns to the Left of and including the IndexNumberCol.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Sub FreezePanes(CWorksheet, CSplitRow)
CWorksheet.Select
CWorksheet.Parent.Windows(1).SplitColumn = 0
CWorksheet.Parent.Windows(1).SplitRow = CSplitRow
CWorksheet.Parent.Windows(1).FreezePanes = True
CWorksheet.Range("A1").Select
End Sub
When you see a strong need for global variables, then use classes.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I tried your version of the FreezePanes Sub - it fails on:
ExcelObject.ActiveWorkbook
Error: Microsoft Excel: Select method of Range class failed
The number of sheets created is an Excel setting. This is not a constant. Thus don't assume that every users has this set to 3. Especially since Excel 2016 you'll find often 1 as corporate default.
In this case, the script is creating the Excel sheet from scratch and I know this works as it's been used for labeling the sheets, among other things. Â But good point to make - and I would assume, if I were editing an excel sheet as opposed to creating it from scratch, the sheet order could change either inadvertently or intentionally causing this to fail miserably.
Also, I just tested with an echo line before verifying that the Range was being constructed properly:
ExcelObject.ActiveWorkbook
is evaluating to:
ExcelObject.ActiveWorkbook
This is the exact code I'm using for the sub:
Sub FreezePanes(SheetNumber) 'Ryan: EE Q 29163471
Wscript.Echo "ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Range(" & "A" & Trim(CStr(Row + 1)) & ").Select"
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Range("A" & Trim(CStr(Row + 1))).Select
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Parent.Windows(1).FreezePanes = True
ExcelObject.ActiveWorkbook.Worksheets(SheetNumber).Range("A1").Select
End Sub






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I believe I've appropriately adapted your code to my script, but am getting a different error:
'Calling code:
FreezePanes ExcelObject.ActiveWorkbook.Worksheets(WorkSheetObject.Name), "B3"
Sub FreezePanes(CWorksheet, CSplitRow) 'Ste5an: EE Q 29163471
WScript.Echo "CWorksheet is an object: " & IsObject(CWorksheet) & ", CSplitRow = " & CSplitRow
CWorksheet.Select
CWorksheet.Parent.Windows(1).SplitColumn = 0
CWorksheet.Parent.Windows(1).SplitRow = CSplitRow
CWorksheet.Parent.Windows(1).FreezePanes = True
CWorksheet.Range("A1").Select
End Sub
Results in:
CWorksheet is an object: True, CSplitRow = B3
M:\Reporting Automation\All Clients\Report.vbs(163, 2) Microsoft Excel: Unable to set the SplitRow property of the Window class
Microsoft Excel
--
Questions
--
Followers
Top Experts
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.