Solved

Freeze Panes VBA Excel

Posted on 2013-05-20
9
3,424 Views
Last Modified: 2013-05-25
Dear Experts

A simple challenge (I hope)

I have code to loop through all the worksheets in a workbook to rearrange column order to make the worksheet more end-user friendly.

A spreadsheet may have up to 52 sheets.

The last piece of code I use is to freeze panes in the correct place.  However, if the sheet is not already scrolled to the correct range, the freeze happens in the middle of the sheet and not at Range ("E4"), as per the code.

The code I use is:
 
 Sub TrackerHeaders()
'
' TrackerHeaders Macro
'
    Dim dStart As Date
    Dim dEnd As Date
    Dim dTime As Long
       
    dStart = Now()
    Dim ws As Worksheet
    Application.ScreenUpdating = False
'loop through Tracker worksheets
    For Each ws In Worksheets '(apart from Summary Sheet)
        If ws.Name <> "Summary" Then
            ws.Activate
            
            If ActiveWindow.FreezePanes Then
                ActiveWindow.FreezePanes = False
            End If
            
            'Cut, paste and rearrange columns
            Columns("Q").Cut
            Range("Z1").Insert shift:=xlToRight
        
            Columns("Z:AF").Cut
            Range("G1:I1").Insert shift:=xlToRight
        
            Columns("AB").Cut
            Range("X1").Insert shift:=xlToRight
            
            Columns("N").Insert shift:=xlToRight, _
                CopyOrigin:=xlFormatFromLeftOrAbove
            
            Range("N3") = "Date figures emailed to Vendor"
            Cells.EntireColumn.autofit
            
            Application.Goto reference:=Range("E4")
            ActiveWindow.FreezePanes = True
            
        End If
    Next ws
    
    Sheets(1).Activate
    dEnd = Now()
    dTime = (dEnd - dStart) * 1440 * 60
    MsgBox "Done in " & dTime & " seconds"
    
End Sub 

Open in new window

RearrangeColumns-Macro.xlsm
0
Comment
Question by:David Phelops
  • 5
  • 3
9 Comments
 
LVL 17

Expert Comment

by:xtermie
ID: 39180292
Why dont you try to do this:
Range("E4").Select
ActiveWindow.FreezePanes = True
0
 

Author Comment

by:David Phelops
ID: 39180299
I thought so too, - I tried that first. Then I tried "Goto". Didn't work, either.
Cheers
David
0
 
LVL 17

Expert Comment

by:xtermie
ID: 39180315
Perhaps we could try to change it a bit so that the worksheet is active
ws.Select
Range("E4").Select
ActiveWindow.FreezePanes = True
0
 
LVL 17

Expert Comment

by:xtermie
ID: 39180325
I tried a simple look within a 5 worksheet workbook like so:

Sub myFreeze()
Dim ws As Worksheet
Application.ScreenUpdating = False
'loop through worksheets
For Each ws In Worksheets
     ws.Select
     Range("E4").Select
     ActiveWindow.FreezePanes = True
Next
End sub

and it worked just fine.

Why dont you just call a separate function at the end of yours that will freeze all sheets in that way?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:David Phelops
ID: 39180350
I'll try that - thanks.  Watch this space.
Cheers
0
 

Author Comment

by:David Phelops
ID: 39180383
Hi extermie

I tried your code... see attached file.  Please run code  as is - you can then see what happens.
I'm puzzled as to why the command
ActiveWindow.FreezePanes = True

Open in new window

does not freeze the panes at the selected cell, but at the centre of the sheet where the active cell was previously.

Would one need to "scroll" the cursor?
Thanks
David
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39180490
Try using:
            Application.Goto reference:=Range("E4")
            With ActiveWindow
               .ScrollColumn = 4
               .ScrollRow = 3
               .FreezePanes = True
            End With

Open in new window

0
 

Assisted Solution

by:David Phelops
David Phelops earned 0 total points
ID: 39180536
Thanks rorya - That works with one slight modification:

Application.Goto reference:=Range("E4")
With ActiveWindow
   .ScrollColumn = 1
   .ScrollRow = 1
   .FreezePanes = True
End With

Open in new window

This allows the Top rows to be seen when the file is opened.

Thanks for your help
0
 

Author Closing Comment

by:David Phelops
ID: 39196259
Simple. Effective.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now