I have the code below, im trying to make my selection of cells print on one a4 page rather than spread across 6 in a command button that is on a sheet, can anyone point out what im doing wrong? I expected to be able to have a printing window like with saving a sheet or something like that, but this is all i can find. thanks a tonne for your help :)
Private Sub CommandButton3_Click()
Sheets("Home").PageSetup.PrintArea = "$A$1:$W$44"
Sheets("Home").PageSetup.Orientation = xlLandscape
Sheets("Home").PageSetup.FitToPagesWide = 1
Sheets("Home").PageSetup.FitToPagesTall = 1
Sheets("Home").PrintPreview (EnableChanges = True)
'Sheets("Home").PrintOut (Preview = True)
End Sub
Ive looked for other some VBA online and looked up the pagesetup, but i cant seem to find what im after, the code above is the closest ive found, but it makes no difference to the printing process.
Is this what you are trying? I have commented the relevant parts of the code. Let me know if you still have any questions.
Option Explicit
Sub Sample()
With ThisWorkbook.Sheets("Home")
With .PageSetup
.PrintArea = "$A$1:$W$44"
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
'~~> To fit text spanning in different pages change the view
ActiveWindow.View = xlPageBreakPreview
'~~> Extend the Horizontal Pagebreak to extreme right
'~~> This mimics what you do when you are dragging the PgBrk
If .HPageBreaks.Count > 0 Then _
.HPageBreaks(1).DragOff Direction:=xlDown, RegionIndex:=1
'~~> Similarly extend the Vertical Pagebreak to extreme down
If .VPageBreaks.Count > 0 Then _
.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
.PrintPreview EnableChanges:=True
'~~> Change the view back to normal
ActiveWindow.View = xlNormalView
End With
End Sub
BEFORE
and the print preview
AFTER
I am working with 5 different buttons that have a single Icon visible in each Button Group. All in 1 Module (If that matters) to hide/unhide row ranges.
So far I have this, but I get a 438 error. I was thinking that I would stack the buttons and toggle them. But I would rather just toggle the icon within the Group.
BTW: I am using Text (No Fill) as the parameter because I don't know another way.
There really isn't much out there that talks about working with Shape Buttons that I can find.
Sub HideInactive()
Call TurnOffStuff 'sub that turns off calculation and screenUpdating
With ActiveSheet.Shapes("HideInactive_Button1")
If .TextFrame.Characters.Text = "Hide" Then 'invisible text on the button
.Shapes("HideInactive_Button2") = HIDE 'Hides the "show" button
StartRow = 13
EndRow = 250
ColNum = 10
For i = StartRow To EndRow
If Cells(i, ColNum).Value = "0" Then
Rows(i).EntireRow.Hidden = True
Else
Rows(i).EntireRow.Hidden = False
End If
Next i
.TextFrame.Characters.Text = "Show"
.Shapes("HideInactive_Button1").Visible = HIDE 'Exposes the "show" button
Range("J13").Select 'moves the active cell to the top of the range
Else
Rows("13:250").EntireRow.Hidden = False 'resets the range view
.TextFrame.Characters.Text = "Hide"
.Shapes.Range("HideInactive_Button2").Visible = HIDE 'Exposes "hide" button
Range("J13").Select
End If
End With
HIDE = Not (HIDE)
Call TurnOnStuff
End Sub
I need something that I can reuse for many buttons just by renaming the shapes and the ranges.
Thank you for the help. I appreciate your time!
Background: This project is gets loaded into full screen maximized.To make it look good I took out the tabs, formula bar, headers and status bar. I created my own buttons customized from shapes. These do have some very attractive colors and 3D views and I am wondering it is my code or the shapes that cause it to load slowly. When I Click a button to change pages is when this happens. Initially I had the button macros simply changing the pages but I thought it was slow because i was using select code. worksheet.Activate and worksheets.range.select. And the code was in the worksheet private sub under the Activate event. Then I took the code from there removed the select criteria and it almost feels slower. Is there anything I can do to pick up speed? Thank you.
This Example shows how the buttons are positioned up top of each worksheet. There are 10 sheets with these buttons which are shapes.
Example of 1 worksheet with Navigation Buttons on top
Here is the code im using now. This code is in public subs for a general module.
Sub StartPg()
'ChangePagesmacro
With wsStart.Application 'initiate page loading
.Calculation = xlCalculationManual 'turn off calculations
.ScreenUpdating = False 'turn off screen updates
.EnableEvents = False 'disable events
.Goto Reference:=wsStart.Range("A4"), Scroll:=True 'page starting point
With .ActiveWindow
If .FreezePanes Then .FreezePanes = False 'unfreeze panes to remove any jam up`enter code here`
.SplitColumn = 0 'unsplit column to free up screen
.SplitRow = 0 'unsplit row to free up screen
.WindowState = xlMaximized 'maximize window
.DisplayHeadings = False 'turn of page headings
.DisplayFormulas = False ' hide formula bar
.DisplayWorkbookTabs = False 'hide page tabs
.DisplayGridlines = False 'hide gridlines
.DisplayHorizontalScrollBar = True 'show scrollbar
.DisplayVerticalScrollBar = True 'show scrollbar
.Zoom = 81 'zoom and area
.ScrollColumn = 1 'scroll to top
.ScrollRow = 1 'scroll to left
.SplitColumn = 0 'split colum to be frozen
.SplitRow = 3 'split row to be frozen
.FreezePanes = True 'freeze panes
End With
End With
With wsStart
.ScrollArea = "SAStart" 'set user scroll area
With .Application
.Calculation = xlCalculationAutomatic 'turn on calculations
.ScreenUpdating = True ' turn on screen updates
.EnableEvents = True ' turn on events
.DisplayFormulaBar = False 'No formula bar
.DisplayStatusBar = False 'No status bar
.DisplayFullScreen = True ' show full screen
End With
End With
End Sub
I recorded a macro to freeze panes, but it is not working as intended. It freezes at the 2nd row and makes the top row hidden. Help. I am using Excel 2007. The code is below:
' Freeze Pains - Top Row
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
I found this code works perfectly:
Sub Freeze_Top_Panes()
Application.ScreenUpdating = False
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Application.ScreenUpdating = True
End Sub
To achieve this for all sheets in your workbook, try this:
Sub Freeze_All()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Application.ActiveWorkbook.Worksheets
Ws.Activate
With Application.ActiveWindow
.FreezePanes = True
End With
Next
Application.ScreenUpdating = True
End Sub
Ii am not sure if this only affects Excel 2007 (as I have not tested it on other versions yet), but it appears that you must turn off screen updating for the freeze panes function to work with VBA. Not sure as to why that is.
If you want to freeze the first row/column, but the first row/column is not displayed on the screen, the macro will not freeze the first row/column as expected.
To solve this issue you have to use the ScrollRow (or Scrollcolumn) property.
Worksheets(1).Cells(1, 1).Select
With ActiveWindow
If .FreezePanes Then .FreezePanes = False
.ScrollRow = 1
.ScrollColumn = 1
.SplitColumn = 5
.SplitRow = 5
.FreezePanes = True
End With
I propose you the snippet enclosed (to freeze after row 5 and column 5)
I have a VBA script in Excel that freezes the panes of an Excel worksheet, but I'm curious to see if this is possible without first selecting a range. This is my current code which freezes rows 1–7 but uses Range.Select:
ActiveSheet.Range("A8").Select
ActiveWindow.FreezePanes = True
Any suggestions?
Record yourself using the View ► Freeze Panes ► Freeze Top Row command and this is what you get for .FreezePanes.
With ActiveWindow
If .FreezePanes Then .FreezePanes = False
.SplitColumn = 0
.SplitRow = 1
.FreezePanes = True
End With
So modifying the .SplitColumn and/or .SplitRow properties should do it for you regardless on what the ActiveCell property is.
There are many things to get wrong about freezing panes. I add my own answer, so I will find it here, and won't have to reinvent it next time.
Public Sub FreezePanesAt(rngDataTopLeft As Range)
Dim wndCurrent As Window
For Each wndCurrent In rngDataTopLeft.Worksheet.Parent.Windows
With wndCurrent
.FreezePanes = False
If Not ((rngDataTopLeft.Row = 1) And (rngDataTopLeft.Column = 1)) Then
.ScrollRow = 1
.ScrollColumn = 1
.SplitRow = rngDataTopLeft.Row - 1
.SplitColumn = rngDataTopLeft.Column - 1
.FreezePanes = True
End If
End With
Next
End Sub
Example usage:
FreezePanesAt ThisWorkbook.Worksheets("Sheet1").Range("B3")
FreezePanesAt ThisWorkbook.Names("Header").RefersToRange
The input parameter is the top left cell of the bottom right pane; I think this is the most frequent use case: you know the range at which to split and don't care about which workbook / worksheet / window it is in
If the input parameter is in the first row / first cell but not A1, then there will be only two panes; A1 is a special case, however, Excel would split the window at center of the current view, I prevented this because I can't think of any case where this would be intended
It iterates through all Windows attached to the workbook / worksheet; indexing into Application.Windows (Windows(Thisworkbook.Name)) won't cause an error if you have more windows to the same workbook (the name would be "MyWorkbook:1"), or Excel attempted (which usually fails) to repair a workbook after a crash (the name would be "MyWorkbook [Repaired]")
It takes into consideration that panes may already be frozen and the user / another macro might have scrolled to a location in the workbook, and the top left cell in the window is not A1
I found the previous answers only worked with some sheets when looping through tabs. I found the following code worked on every tab I looped through (target was a single workbook), despite which workbook was the activeworkbook.
The short of it:
With Application.Windows(DataWKB.Name)
Application.Goto ws.Cells(4, 5)
.SplitColumn = 4
.SplitRow = 3
.FreezePanes = True
End With
The code as it is in my Sub: (be aware, I do a lot more formatting in this sub, I tried to strip that out and leave just the code needed here)
Sub Format_Final_Report()
Dim DataWKB As Workbook
Set DataWKB = Workbooks("Report.xlsx")
Dim ws As Worksheet
Dim tabCNT As Long
Dim tabName As String
tabCNT = DataWKB.Sheets.Count
For i = 1 To tabCNT
Set ws = DataWKB.Worksheets(i)
tabName = ws.Name
With Application.Windows(DataWKB.Name)
Application.Goto ws.Cells(4, 5)
.SplitColumn = 4
.SplitRow = 3
.FreezePanes = True
End With
Next i
End Sub
Hopefully, this will save someone some research time in the future.
I need to be able to properly refreeze panes (when creating new windows, notably) without losing the activecell or messing up the visible range. It took a lot of playing around but I think I have something solid that works:
Sub FreezePanes(nbLignes As Integer, nbColonnes As Integer, Optional ByVal feuille As Worksheet)
If feuille Is Nothing Then Set feuille = ActiveSheet Else feuille.Activate
Error GoTo erreur
With ActiveWindow
If .View = xlNormalView Then
If .FreezePanes Then .FreezePanes = False
If .Split Then .Split = False
.SplitColumn = nbColonnes
.SplitRow = nbLignes
If .Panes.Count = 4 Then 'rows and columns frozen
.Panes(1).ScrollRow = 1
.Panes(1).ScrollColumn = 1
.Panes(2).ScrollRow = 1 'top right pane
.Panes(3).ScrollColumn = 1 'bottom left pane
ElseIf nbLignes > 0 Then .Panes(1).ScrollRow = 1
ElseIf nbColonnes > 0 Then .Panes(1).ScrollColumn = 1
Else: GoTo erreur
End If
.FreezePanes = True
End If
End With
Exit Sub
erreur:
Debug.print "Erreur en exécutant le sub 'FreezePanes " & nbLignes & ", " & nbColonnes & ", '" & feuille.Name & "' : code #" & Err.Number & Err.Description
End Sub
I know this is old but I came across this tidbit that may be useful...
as ChrisB stated, the SplitColumn/SplitRow values represent the last cell above/left of the split BUT of the currently visible window. So if you happen to have code like this:
Application.Goto Worksheets(2).Range("A101"), True
With ActiveWindow
.SplitColumn = 0
.SplitRow = 10
.FreezePanes = True
End With
The split will be between rows 110 and 111 instead of 10 and 11.
edited for clarification and to add more information:
My point is that the values are offsets of the upper left cell, not an address of a cell. Therefore, ChrisB's Dec 4 '15 at 18:34 comment under the main answer only holds if row 1 is visible in the Activewindow.
A couple of other points on this:
using Application.goto doesn't necessarily put whichever cell you
are trying to go to in the upper left
the cell that is put in the upper left when using .goto can depend
on the size of the excel window, the current zoom level, etc (so fairly arbitrary)
it is possible to have the splits placed so that you can not see
them or even scroll around in the visible window (if .FreezePanes =
true). for example:
Application.Goto Worksheets(1).Range("A1"), True
With ActiveWindow
.SplitColumn = 100
.SplitRow = 100
.FreezePanes = True
End With
CETAB may be dealing with this in their answer.
Yes, the ActiveWindow.ScrollRow = 1 and ActivWindow.ScrollColumn = 1 is a must for FreezePanes if your visible window does not include cell A1.
If you are freezing rows 1:3 by selecting row 4 or cell A4, and cell A3 is not visible, the FreezePanes function will freeze the window in the center of the visible window.
Also if cell B4 is selected, and column A is not visible, then only the rows 1:3 will be frozen (column A will not frozen). Similarly, if rows 1:3 are not visible, only column A will be frozen. If both column A and rows 1:3 are not visible, the FreezePanes function will freeze the window in the center of the visible window.
The problem with splitting is that if a user unfreezes panes, the panes will remain split. (I couldn't find a way to turn off split afterwards while keeping the panes frozen)
This may be too obvious/simple, but what if the current selection is simply saved and then re-selected afterwards?
Sub FreezeTopRow()
'First save the current selection to go back to it later
Dim rngOriginalSelection As Range
Set rngOriginalSelection = Selection
'Change selection to A2 to make .FreezePanes work
ActiveSheet.Range("A2").Select
ActiveWindow.FreezePanes = True
'Change selection back to original
rngOriginalSelection.Select
End Sub
Here is what i use...
Public Sub FreezeTopRowPane(ByRef MyWs As Excel.Worksheet, _
Optional ByVal AfterRowNr As Integer = 1)
Dim SavedWS As Worksheet
Dim SavedUpdating As Boolean
SavedUpdating = Application.ScreenUpdating 'save current screen updating mode
Set SavedWS = ActiveSheet 'save current active sheet
Application.ScreenUpdating = False 'turn off screen updating
MyWs.Activate 'activate worksheet for panes freezing
ActiveWindow.FreezePanes = False 'turn off freeze panes in case
With ActiveWindow
.SplitColumn = 0 'set no column to split
.SplitRow = AfterRowNr 'set the row to split, default = row 1
End With
ActiveWindow.FreezePanes = True 'trigger the new pane freezing
SavedWS.Activate 'restore previous (saved) ws as active
Application.ScreenUpdating = SavedUpdating 'restore previous (saved) updating mode
End Sub
I did a timing test of Freezing using .Select vs .Activate. Here is the code
Dim numLoops As Long
Dim StartTime, LoopTime As Long
numLoops = 1000
Debug.Print ("Timing test of numloops:" & numLoops)
StartTime = Timer
For I = 0 To numLoops
targetSheet.Activate
With ActiveWindow
If .FreezePanes Then .FreezePanes = False
.SplitColumn = 2
.SplitRow = 1
.FreezePanes = True
End With
Next I
LoopTime = Timer
Debug.Print ("Total time of activate method:" & Format((LoopTime - StartTime) / 86400, "hh:mm:ss"))
StartTime = Timer
For I = 0 To numLoops
targetSheet.Select
Application.Range("C2").Select
Application.ActiveWindow.FreezePanes = True
Next I
LoopTime = Timer
Debug.Print ("Total time of select method:" & Format((LoopTime - StartTime) / 86400, "hh:mm:ss"))
And here are the results.
Timing test of numloops:1000
Total time of activate method:00:00:39
Total time of select method:00:00:01
As you can see, .Select is much faster.