PC Review


Reply
Thread Tools Rate Thread

adjust zoom according to sheet

 
 
vicky
Guest
Posts: n/a
 
      3rd Nov 2009
i am newbie to vba programming ... i need to set the zoom according to
the sheets in such a way that content present in the page should
exactly fit in one page ... if i need to provide further details pls
let me know.
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      3rd Nov 2009
Sub FitDataToWindow()
Dim ratio As Double
Dim rw As Long, col As Long
Dim cTL As Range, cBR As Range, rData As Range
Dim wn As Window

LastDcell ActiveSheet, rw, col, False
Set cTL = Cells(rw, col)

LastDcell ActiveSheet, rw, col, True
Set cBR = Cells(rw, col)

Set rData = Range(cTL, cBR)
Set cTL = rData(1)
Set cBR = rData(rData.Cells.Count)

Application.Goto cTL, True

Set wn = ActiveWindow
wn.Zoom = 100

With wn.VisibleRange

ratio = .Resize(, .Columns.Count - 1).Width / rData.Width

If (ratio > .Resize(.Rows.Count - 1).Height / rData.Height) Then
ratio = .Resize(.Rows.Count - 1).Height / rData.Height
' will zoom to height
End If

End With

' zoom can be betweeen 10-400
If ratio > 4 Then ratio = 4
If ratio < 0.1 Then ratio = 0.1 ' can't show all data!

wn.Zoom = Int(ratio * 100)

If ratio > 0.1 Then
' might need to reduce zoom slightly if last cell not in window
If Intersect(wn.VisibleRange, cBR) Is Nothing Then
wn.Zoom = wn.Zoom - 1
End If
End If

End Sub


Function LastDcell(ws As Worksheet, dR As Long, dc As Long, _
bLastCell As Boolean) As Boolean
Dim x
Dim SrchDir As XlSearchDirection

If bLastCell Then
SrchDir = xlPrevious
Else
SrchDir = xlNext
End If

On Error GoTo errH

With ws.Cells
dc = .Find("*", .Range("A1"), xlFormulas, xlPart, _
xlByColumns, SrchDir, 0).Column
dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
xlByColumns, SrchDir, 0).Row
x = .Find("") 'reset Find
End With

Exit Function
errH:
' typically empty sheet
dR = 1
dc = 1
End Function


Only light tested ...

Regards,
Peter T

"vicky" <(E-Mail Removed)> wrote in message
news:11527497-6c53-451a-a7da-(E-Mail Removed)...
>i am newbie to vba programming ... i need to set the zoom according to
> the sheets in such a way that content present in the page should
> exactly fit in one page ... if i need to provide further details pls
> let me know.



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      3rd Nov 2009
TYPO !

in LastDcell() change
dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
xlByColumns, SrchDir, 0).Row
to

dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
xlByRows, SrchDir, 0).Row

With that corrected can get rid of
Set cTL = rData(1)
Set cBR = rData(rData.Cells.Count)

Regards,
Peter T

"Peter T" <peter_t@discussions> wrote in message
news:%(E-Mail Removed)...
> Sub FitDataToWindow()
> Dim ratio As Double
> Dim rw As Long, col As Long
> Dim cTL As Range, cBR As Range, rData As Range
> Dim wn As Window
>
> LastDcell ActiveSheet, rw, col, False
> Set cTL = Cells(rw, col)
>
> LastDcell ActiveSheet, rw, col, True
> Set cBR = Cells(rw, col)
>
> Set rData = Range(cTL, cBR)
> Set cTL = rData(1)
> Set cBR = rData(rData.Cells.Count)
>
> Application.Goto cTL, True
>
> Set wn = ActiveWindow
> wn.Zoom = 100
>
> With wn.VisibleRange
>
> ratio = .Resize(, .Columns.Count - 1).Width / rData.Width
>
> If (ratio > .Resize(.Rows.Count - 1).Height / rData.Height) Then
> ratio = .Resize(.Rows.Count - 1).Height / rData.Height
> ' will zoom to height
> End If
>
> End With
>
> ' zoom can be betweeen 10-400
> If ratio > 4 Then ratio = 4
> If ratio < 0.1 Then ratio = 0.1 ' can't show all data!
>
> wn.Zoom = Int(ratio * 100)
>
> If ratio > 0.1 Then
> ' might need to reduce zoom slightly if last cell not in window
> If Intersect(wn.VisibleRange, cBR) Is Nothing Then
> wn.Zoom = wn.Zoom - 1
> End If
> End If
>
> End Sub
>
>
> Function LastDcell(ws As Worksheet, dR As Long, dc As Long, _
> bLastCell As Boolean) As Boolean
> Dim x
> Dim SrchDir As XlSearchDirection
>
> If bLastCell Then
> SrchDir = xlPrevious
> Else
> SrchDir = xlNext
> End If
>
> On Error GoTo errH
>
> With ws.Cells
> dc = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> xlByColumns, SrchDir, 0).Column
> dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> xlByColumns, SrchDir, 0).Row
> x = .Find("") 'reset Find
> End With
>
> Exit Function
> errH:
> ' typically empty sheet
> dR = 1
> dc = 1
> End Function
>
>
> Only light tested ...
>
> Regards,
> Peter T
>
> "vicky" <(E-Mail Removed)> wrote in message
> news:11527497-6c53-451a-a7da-(E-Mail Removed)...
>>i am newbie to vba programming ... i need to set the zoom according to
>> the sheets in such a way that content present in the page should
>> exactly fit in one page ... if i need to provide further details pls
>> let me know.

>
>



 
Reply With Quote
 
vicky
Guest
Posts: n/a
 
      4th Nov 2009
On Nov 3, 5:36*pm, "Peter T" <peter_t@discussions> wrote:
> TYPO !
>
> in LastDcell() change
> * * * * dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> * * * * * * * * * * * * * * * * * * xlByColumns, SrchDir, 0).Row
> to
>
> * * * * dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> * * * * * * * * * * * * * * * * * * xlByRows, SrchDir, 0).Row
>
> With that corrected can get rid of
> * * Set cTL = rData(1)
> * * Set cBR = rData(rData.Cells.Count)
>
> Regards,
> Peter T
>
> "Peter T" <peter_t@discussions> wrote in message
>
> news:%(E-Mail Removed)...
>
>
>
> > Sub FitDataToWindow()
> > Dim ratio As Double
> > Dim rw As Long, col As Long
> > Dim cTL As Range, cBR As Range, rData As Range
> > Dim wn As Window

>
> > * *LastDcell ActiveSheet, rw, col, False
> > * *Set cTL = Cells(rw, col)

>
> > * *LastDcell ActiveSheet, rw, col, True
> > * *Set cBR = Cells(rw, col)

>
> > * *Set rData = Range(cTL, cBR)
> > * *Set cTL = rData(1)
> > * *Set cBR = rData(rData.Cells.Count)

>
> > * *Application.Goto cTL, True

>
> > * *Set wn = ActiveWindow
> > * *wn.Zoom = 100

>
> > * *With wn.VisibleRange

>
> > * * * *ratio = .Resize(, .Columns.Count - 1).Width / rData.Width

>
> > * * * *If (ratio > .Resize(.Rows.Count - 1).Height / rData.Height) Then
> > * * * * * *ratio = .Resize(.Rows.Count - 1).Height / rData.Height
> > * * * * * *' will zoom to height
> > * * * *End If

>
> > * *End With

>
> > * *' zoom can be betweeen 10-400
> > * *If ratio > 4 Then ratio = 4
> > * *If ratio < 0.1 Then ratio = 0.1 ' can't show all data!

>
> > * *wn.Zoom = Int(ratio * 100)

>
> > * *If ratio > 0.1 Then
> > * * * *' might need to reduce zoom slightly if last cell not inwindow
> > * * * *If Intersect(wn.VisibleRange, cBR) Is Nothing Then
> > * * * * * *wn.Zoom = wn.Zoom - 1
> > * * * *End If
> > * *End If

>
> > End Sub

>
> > Function LastDcell(ws As Worksheet, dR As Long, dc As Long, _
> > * * * * * * * * * * * *bLastCell As Boolean) AsBoolean
> > Dim x
> > Dim SrchDir As XlSearchDirection

>
> > * *If bLastCell Then
> > * * * *SrchDir = xlPrevious
> > * *Else
> > * * * *SrchDir = xlNext
> > * *End If

>
> > * *On Error GoTo errH

>
> > * *With ws.Cells
> > * * * *dc = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> > * * * * * * * * * * * * * * * * * *xlByColumns, SrchDir, 0).Column
> > * * * *dR = .Find("*", .Range("A1"), xlFormulas, xlPart, _
> > * * * * * * * * * * * * * * * * * *xlByColumns, SrchDir, 0).Row
> > * * * *x = .Find("") * *'reset Find
> > * *End With

>
> > * *Exit Function
> > errH:
> > ' typically empty sheet
> > * *dR = 1
> > * *dc = 1
> > End Function

>
> > Only light tested ...

>
> > Regards,
> > Peter T

>
> > "vicky" <vimalbarl...@gmail.com> wrote in message
> >news:11527497-6c53-451a-a7da-(E-Mail Removed)....
> >>i am newbie to vba programming ... i need to set the zoom according to
> >> the sheets in such a way that content present in the page should
> >> exactly fit in one page ... if i need to provide further details pls
> >> let me know.- Hide quoted text -

>
> - Show quoted text -


thanks a lot peter .
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is the zoom control box I want to adjust width NORM Microsoft Word New Users 2 12th May 2010 04:23 AM
unwanted: Zoom adjust when adjusting Print Scale skiing Microsoft Excel Discussion 8 31st Mar 2009 11:32 PM
Can a macro be written that when you open a workbook the zoom will be set for each sheet? Marc Microsoft Excel Programming 5 6th Apr 2006 02:14 AM
Setting Sheet Display Zoom Magnification in VBA =?Utf-8?B?S2V2aW4=?= Microsoft Excel Programming 5 16th Mar 2005 07:56 PM
Auto Adjust Spread Sheet so it will print on 1 page/legal/landscap =?Utf-8?B?UGFtIDop?= Microsoft Excel Worksheet Functions 0 4th Mar 2005 08:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 AM.