使用Excel VBA更改视图

时间:2022-11-19 23:08:10

I'm trying to run a simple For Each loop to change the view in Excel, but keep getting a:

我正在尝试运行一个简单的For Each循环来更改Excel中的视图,但不断获得:

438 Run-time Error

438运行时错误

Sub ChangeView()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.Select
    ActiveWindow = xlNormalView
    Range("A1").Select
Next

The error highlights the ActiveWindow line. Why?

该错误突出显示ActiveWindow行。为什么?

2 个解决方案

#1


8  

ActiveWindow is an object, xlNormalView is a long/enumerated constant associated with a property of that object. You can't assign the property value to the object itself, instead, assign it to the appropriate property. I believe the correct one would be:

ActiveWindow是一个对象,xlNormalView是一个与该对象的属性相关联的长/枚举常量。您无法将属性值分配给对象本身,而是将其分配给相应的属性。我相信正确的是:

ActiveWindow.View = xlNormalView 

#2


0  

The following VBA will set all SELECTED sheets to the same view, same zoom level and same visible area as the active tab:

以下VBA将所有SELECTED工作表设置为与活动选项卡相同的视图,相同的缩放级别和相同的可见区域:

Sub ResetAllSheetPerspectives_OnSelectedSheets()
    'Not working? Are any tabs selected? Only works on selected worksheets.
    Dim ws As Worksheet
    Dim lRow As Long
    Dim lCol As Long
    Dim dZoom, dView As Double

    lRow = ActiveWindow.ScrollRow
    lCol = ActiveWindow.ScrollColumn
    dZoom = ActiveWindow.Zoom
    dView = ActiveWindow.View

    For Each ws In Application.ActiveWindow.SelectedSheets
        ws.Activate
        ActiveWindow.Zoom = dZoom
        ActiveWindow.View = dView
        Application.Goto ws.Cells(lRow, lCol), True
    Next ws
End Sub

Please upvote the below source:

请提供以下来源:

Setting all selected sheets to same visible area

将所有选定的纸张设置为相同的可见区域


See Also:

也可以看看:

How to run a macro in Excel - support.office.com

如何在Excel中运行宏 - support.office.com

Use macros in Excel - WikiHow

在Excel中使用宏 - WikiHow

How to use macros you find online in six easy steps - searchengineland.com

如何使用您通过六个简单步骤在线找到的宏 - searchengineland.com

#1


8  

ActiveWindow is an object, xlNormalView is a long/enumerated constant associated with a property of that object. You can't assign the property value to the object itself, instead, assign it to the appropriate property. I believe the correct one would be:

ActiveWindow是一个对象,xlNormalView是一个与该对象的属性相关联的长/枚举常量。您无法将属性值分配给对象本身,而是将其分配给相应的属性。我相信正确的是:

ActiveWindow.View = xlNormalView 

#2


0  

The following VBA will set all SELECTED sheets to the same view, same zoom level and same visible area as the active tab:

以下VBA将所有SELECTED工作表设置为与活动选项卡相同的视图,相同的缩放级别和相同的可见区域:

Sub ResetAllSheetPerspectives_OnSelectedSheets()
    'Not working? Are any tabs selected? Only works on selected worksheets.
    Dim ws As Worksheet
    Dim lRow As Long
    Dim lCol As Long
    Dim dZoom, dView As Double

    lRow = ActiveWindow.ScrollRow
    lCol = ActiveWindow.ScrollColumn
    dZoom = ActiveWindow.Zoom
    dView = ActiveWindow.View

    For Each ws In Application.ActiveWindow.SelectedSheets
        ws.Activate
        ActiveWindow.Zoom = dZoom
        ActiveWindow.View = dView
        Application.Goto ws.Cells(lRow, lCol), True
    Next ws
End Sub

Please upvote the below source:

请提供以下来源:

Setting all selected sheets to same visible area

将所有选定的纸张设置为相同的可见区域


See Also:

也可以看看:

How to run a macro in Excel - support.office.com

如何在Excel中运行宏 - support.office.com

Use macros in Excel - WikiHow

在Excel中使用宏 - WikiHow

How to use macros you find online in six easy steps - searchengineland.com

如何使用您通过六个简单步骤在线找到的宏 - searchengineland.com