控制Excel窗口大小和形状大小VBA

时间:2015-03-09 15:47:57

标签: excel vba excel-vba

我有几行代码来操作excel窗口的窗口大小,使窗口与形状的大小匹配(命名为背景)

我的问题是,形状和窗口的高度和宽度是不匹配的。

我有什么遗失的东西吗?像素不应等于像素吗?

工作簿激活模块:

Private Sub Workbook_Activate()
' Hide Ribbon and Controls
Dim Background As Shape

Set Background = ThisWorkbook.ActiveSheet.Shapes("Background")

With Application
    .ScreenUpdating = False
    .ExecuteExcel4Macro "SHOW.TOOLBAR(""RIBBON"", FALSE)"
    .DisplayFormulaBar = False
    .WindowState = xlNormal
    .Height = Background.Height
    .Width = Background.Width
    .DisplayStatusBar = False
End With
With ActiveWindow
    .DisplayHeadings = False
    .DisplayWorkbookTabs = False
    .DisplayHeadings = False
    .DisplayHorizontalScrollBar = False
    .DisplayVerticalScrollBar = False
    .ScrollColumn = 1
    .ScrollRow = 1
End With
End Sub

工作簿取消激活模块:

Private Sub Workbook_Deactivate()
' Show all Controls and Ribbon
With Application
    .ScreenUpdating = False
    .ExecuteExcel4Macro "SHOW.TOOLBAR(""RIBBON"", TRUE)"
    .DisplayFormulaBar = True
End With
With ActiveWindow
    .DisplayHeadings = True
    .DisplayWorkbookTabs = True
    .DisplayHeadings = True
    .DisplayHorizontalScrollBar = True
    .DisplayVerticalScrollBar = True
End With
End Sub

工作表更改模块:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim Background As Shape

Set Background = Nothing
On Error Resume Next
Set Background = Sh.Shapes("Background")
On Error GoTo 0

If Not Background Is Nothing Then
    With Application
        .WindowState = xlNormal
        .Height = Background.Height
        .Width = Background.Width
    End With
    With ActiveWindow
        .ScrollColumn = 1
        .ScrollRow = 1
    End With
End If
End Sub

有人可以帮助解决这些不匹配的原因吗?

非常感谢

编辑添加的屏幕截图:背景是黑色方块(为了保密目的,必须删除实际图像)。边缘周围的白色是excel背景

Excel Screenshot

2 个答案:

答案 0 :(得分:0)

试试这个,让我知道你的想法:

行后:

Set Background = ThisWorkbook.ActiveSheet.Shapes("Background")

添加:

Background.Left = 0
Background.Top = 0

答案 1 :(得分:0)

可能是因为您在删除滚动条和工作簿标签等之前设置了窗口大小。看到的空白可能是这些滚动条占用的空间。