5

画面の解像度に基づいて画面のズームを調整するExcel2003マクロがあります。

Sub Macro1()
   Dim maxWidth As Long, myWidth As Long
   Dim myZoom As Single

   maxWidth = Application.UsableWidth * 0.96
   'I use r because upto r i have macro buttons
   myWidth = ThisWorkbook.ActiveSheet.Range("r1").Left
   myZoom = maxWidth / myWidth
   ActiveWindow.Zoom = myZoom * 100
End Sub

Excel 2003で試してみると、ボタンのサイズとそのキャプションが正しくズームされていません。また、画面解像度1024*768または1366*768のいずれかの幅としてApplication.UsableWidth常に返されます。1026何か案は?

システムの画面解像度で開いた場合、Excelシートを幅に合わせたい

4

3 に答える 3

11
Sheets(1).Range("a1:AC1").Select
ActiveWindow.Zoom = True

はい、必要なのはこれだけです。これにより、画面の解像度に基づいてズームレベルが調整されます。詳細については、以下のリンクを参照してください:-http: //optionexplicitvba.blogspot.sg/2011/10/one-size-fits-all.html

于 2013-10-17T23:43:37.530 に答える
4

このWindowsAPI呼び出しをコードに追加して、画面の解像度を決定できます。

Private Declare PtrSafe Function GetSystemMetrics Lib "USER32" _
 (ByVal nIndex As Long) As Long

  Sub Macro1()
    Dim maxWidth As Long
    Dim myWidth As Long
    Dim myZoom As Single

    maxWidth = GetSystemMetrics(0) * 0.96
    myWidth = ThisWorkbook.ActiveSheet.Range("R1").Left
    myZoom = maxWidth / myWidth
    ActiveWindow.Zoom = myZoom * 100

  End Sub
于 2012-07-23T11:12:23.833 に答える
0

複数のシートに使用できるようにまとめたものを共有したいと思いました。上記の回答から借用しており、アクティブ範囲を指定する必要はありません。

Sub Zoomitgood()

'this macro will loop through all the sheets and zoom to fit the contents by 
'measuring the width and height of each sheet. It will then zoom to 90% of 
'the "zoom to fit" setting.


    Dim WS_Count As Integer
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim maxwidth As Integer
    Dim width As Integer
    Dim Height As Integer
    Dim MaxHeight As Integer
    Dim zoom As Integer

'First Loop: Loop through each sheet, select each sheet so that each width 
'and height can be measured. The width and height are measured in number of 
'cells.

WS_Count = ActiveWorkbook.Worksheets.Count

For i = 1 To WS_Count

Worksheets(i).Activate
maxwidth = 0
MaxHeight = 0

'Second loop: measure the width of each sheet by running line by line and 
'finding the rightmost cell. The maximum value of the rightmost cell will be 
'set to the maxwidth variable

For j = 1 To 100
width = Cells(j, 100).End(xlToLeft).Column
If width >= maxwidth Then

maxwidth = width

End If

Next

'Third loop: measure the height of each sheet by running line by line and 
'finding the rightmost cell. The maximum value of the lowest cell will be 
'set to the maxheight variable.

For k = 1 To 100
Height = Cells(100, k).End(xlUp).Row
If Height >= MaxHeight Then

MaxHeight = Height

End If

Next

'Finally, back to loop 1, select the range for zooming. Then set the zoom to 
'90% of full zoom.

Range(Cells(1, 1), Cells(MaxHeight, maxwidth)).Select
ActiveWindow.zoom = True
zoom = ActiveWindow.zoom
ActiveWindow.zoom = zoom * 0.9
Cells(1000, 1000).Select
Application.CutCopyMode = False
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1

Next

MsgBox "You have been zoomed"


Application.ScreenUpdating = True
Application.DisplayAlerts = True



 End Sub
于 2017-10-26T15:56:33.460 に答える