Solved

Freeze Panes VBA Excel

Posted on 2013-05-20
9
3,716 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 18

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 18

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

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
 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

808 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