gpt4 book ai didi

vba - 更改图表位置的代码需要激活我的目标表吗?

转载 作者:行者123 更新时间:2023-12-02 19:06:58 24 4
gpt4 key购买 nike

我使用此代码动态更改图表的大小及其在工作表上的位置。当我每天添加新的数据行时,我会将页面上的一堆图表向下移动以与新的最后一行对齐。我不明白的是为什么我需要激活带有图表的工作表才能使其正常工作。

在有人询问之前,我的 lastrow 值已被显式检索,因此我的值都不会从 ActiveSheet 中解释。但是,我仍然遇到这个问题...这是我的子例程,它需要一些参数来调整特定图表:

Private Sub FitChart(mainsheet As Worksheet, mainchart As String, firstcol As String, secondcol As String, topoffset As Integer, bottomoffset As Integer, lastrow As Long)
'This sub is used to line up the charts with the lastrow of data on each sheet

mainsheet.ChartObjects(mainchart).Chart.Parent.Height = mainsheet.Range(firstcol & lastrow + topoffset & ":" & secondcol & lastrow + bottomoffset).Height
mainsheet.ChartObjects(mainchart).Chart.Parent.Width = mainsheet.Range(firstcol & lastrow + topoffset & ":" & secondcol & lastrow + bottomoffset).Width
mainsheet.ChartObjects(mainchart).Chart.Parent.Top = mainsheet.Range(firstcol & lastrow + topoffset & ":" & secondcol & lastrow + bottomoffset).Top
mainsheet.ChartObjects(mainchart).Chart.Parent.Left = mainsheet.Range(firstcol & lastrow + topoffset & ":" & secondcol & lastrow + bottomoffset).Left

End Sub

以下是我如何调用它的示例:

'Worksheets("main chart").Activate

'Change chart position
Call FitChart(Worksheets("main chart"), "Chart 92", "W", "AH", -30, -1, lastrow)

Activate行注释掉后,图表排列不均匀 - 请注意,红线是我想要与我的lastrow数据对齐的线:

Image1

但是当我取消注释 Activate 行时:

Image2

它排列完美 - 是什么给了???这虽然很小,但它的功能却让我抓狂。我还有 50 多个工作表正在使用此代码,如果我按顺序运行,则会有很多不必要的激活...

最佳答案

正如问题评论中所指出的,当工作表A)未激活且B)未设置为100%缩放时,这是一个错误。

事件工作表的缩放比例无关紧要 - 100%、200%、50%,与带有图表的工作表相同:这些都不重要。

以下代码将采用 SheetName 作为字符串,并让您在不更改 ActiveSheet 的情况下更改缩放。但是,它选择和取消选择工作表。

Sub ChangeZoom(ResetSheet As String, Optional NewZoom As Double = 100)
Dim CurrentZoom As Variant, CurrentSheet As String
CurrentZoom = ActiveWindow.Zoom 'Store current Zoom
CurrentSheet = ActiveSheet.Name 'Store current Sheet
ThisWorkbook.Sheets(Array(CurrentSheet, ResetSheet)).Select 'Select current sheet and Sheet to Zoom
ActiveWindow.Zoom = NewZoom 'Change Zoom
ThisWorkbook.Sheets(CurrentSheet).Select 'Select just the current sheet
ActiveWindow.Zoom = CurrentZoom 'Restore the original Zoom
End Sub

关于vba - 更改图表位置的代码需要激活我的目标表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49493633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com