?
Solved

Freeze Panes VBA Excel

Posted on 2013-05-20
9
Medium Priority
?
4,400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

770 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