Solved

Freeze Panes VBA Excel

Posted on 2013-05-20
9
3,911 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

730 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