Article ID: 149241
Article Last Modified on 8/17/2005
Dim LeftCol, LastCol, TopRow, LastRow, NumCols, _
NumRows, Cadd, Radd As Integer
Dim ScreenTop, ScreenLeft, ScreenHeight, ScreenWidth, _
ADJUST_BOTTOM_UPWARDS, ADJUST_RIGHT_SIDE_LEFTWARD, _
ADJUST_TOP_DOWNWARDS, ADJUST_LEFT_SIDE_RIGHTWARD, _
oldW, oldH, OldCw, OldCsw, OldRh, newW, newH, _
Cinc, Rinc, Builder As Double
' Enter custom adjustments here, if needed.
Sub CUSTOM_ADJUSTMENTS()
ADJUST_TOP_DOWNWARDS = 0
ADJUST_BOTTOM_UPWARDS = 0
ADJUST_LEFT_SIDE_RIGHTWARD = 0
ADJUST_RIGHT_SIDE_LEFTWARD = 0
End Sub
Sub Get_Visible_Area()
Application.ScreenUpdating = False
CUSTOM_ADJUSTMENTS
Sheets("Sheet1").Select
' Find minimum effective ColumnWidth increment.
' ============================================
oldW = Columns(1).ColumnWidth
Builder = 0.001
' Try to widen column by incrementally larger
' amounts until it gets Builder:
Do Until Columns(1).ColumnWidth > oldW
Columns(1).ColumnWidth = Columns(1).ColumnWidth + Builder
Builder = Builder + 0.001
Loop
newW = Columns(1).ColumnWidth
' Return column to its original width:
Columns(1).ColumnWidth = oldW
Cinc = Application.RoundUp(newW - oldW, 2)
' Find minimum effective RowHeight increment
' ==========================================
oldH = Rows(1).RowHeight
Builder = -0.001
' Try to make row shorter by incrementally larger
' amounts until it actually gets shorter:
Do Until Rows(1).RowHeight < oldH
Rows(1).RowHeight = Rows(1).RowHeight + Builder
Builder = Builder - 0.001
Loop
newH = Rows(1).RowHeight
' Return row to its original height:
Rows(1).RowHeight = oldH
Rinc = -Application.RoundDown(newH - oldH, 2)
' Get Top.
' =======
ScreenTop = ActiveWindow.VisibleRange.Rows(1).Top + _
ADJUST_TOP_DOWNWARDS
' Get Left.
' ========
ScreenLeft = ActiveWindow.VisibleRange.Columns(1).Left + _
ADJUST_LEFT_SIDE_RIGHTWARD
' Get Width.
' =========
LeftCol = ActiveWindow.VisibleRange.Columns(1).Column
NumCols = ActiveWindow.VisibleRange.Columns.Count - 1
' If only one big column is visible:
If NumCols = 0 Then
Set LastCol = Columns(LeftCol)
Cinc = Cinc * -1
Else
Set LastCol = Columns(LeftCol + NumCols - 1)
End If
OldCw = LastCol.ColumnWidth
OldCsw = Columns(LastCol.Column).Width
' Change the column width until a column border crosses the
' right edge of the screen:
Do Until ActiveWindow.VisibleRange.Columns.Count <> NumCols + 1
LastCol.ColumnWidth = LastCol.ColumnWidth + Cinc
Loop
' A small adjustment; your screen may vary:
LastCol.ColumnWidth = LastCol.ColumnWidth + Abs(Cinc * 2)
' Add up the column widths:
For Cadd = LeftCol To LastCol.Column
ScreenWidth = ScreenWidth + Columns(Cadd).Width
Next
' Return the column to its original width:
LastCol.ColumnWidth = OldCw - _
ADJUST_RIGHT_SIDE_LEFTWARD - ADJUST_LEFT_SIDE_RIGHTWARD
' Get Height.
' ==========
TopRow = ActiveWindow.VisibleRange.Rows(1).Row
NumRows = ActiveWindow.VisibleRange.Rows.Count - 1
' If only one big row is visible:
If NumRows = 0 Then
Set LastRow = Rows(TopRow)
Rinc = Rinc * -1
Else
Set LastRow = Rows(TopRow + NumRows - 1)
End If
OldRh = LastRow.RowHeight
' Change the row height until a row border crosses the
' bottom edge of the screen:
Do Until ActiveWindow.VisibleRange.Rows.Count <> NumRows + 1
LastRow.RowHeight = LastRow.RowHeight + Rinc
Loop
' A small adjustment; your screen may vary:
LastRow.RowHeight = LastRow.RowHeight + Abs(Rinc * 2)
' Add up the row heights:
For Radd = TopRow To LastRow.Row
ScreenHeight = ScreenHeight + Rows(Radd).Height
Next
' Return the row to its original height:
LastRow.RowHeight = OldRh - _
ADJUST_BOTTOM_UPWARDS - ADJUST_TOP_DOWNWARDS
' Sanity check.
' ============
If ScreenWidth < 0 Then
MsgBox "Cannot create rectangle." & Chr(13) & Chr(13) _
& "ADJUST_TOP_DOWNWARDS and/or " & _
"ADJUST_BOTTOM_UPWARDS is too high."
ElseIf ScreenHeight < 0 Then
MsgBox "Cannot create rectangle." & Chr(13) & Chr(13) _
& "ADJUST_LEFT_SIDE_RIGHTWARD and/or " & _
"ADJUST_RIGHT_SIDE_LEFTWARD is too high."
Else
' Create an example object filling the viewable area.
' ==================================================
ActiveSheet.Rectangles.Add(ScreenLeft, ScreenTop, ScreenWidth, _
ScreenHeight).Select
End If
End Sub
176476 OFF: Office Assistant Not Answering Visual Basic Questions
VisibleRange
Additional query words: 5.00a 8.00 XL5 XL7 XL97 XL98 viewable showing shown unhidden not hidden appearing seeable exposed unconcealed displayed XL
Keywords: kbhowto kbprogramming kbdtacode KB149241