Solved

Freeze Panes VBA Excel

Posted on 2013-05-20
9
3,261 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
Comment Utility
Why dont you try to do this:
Range("E4").Select
ActiveWindow.FreezePanes = True
0
 

Author Comment

by:David Phelops
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

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

Author Comment

by:David Phelops
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Simple. Effective.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

763 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

7 Experts available now in Live!

Get 1:1 Help Now