Code that changes chart position requires my target sheet to be Activated?

两盒软妹~` 提交于 2020-01-13 18:24:09

问题


I use this code to dynamically change the size of a chart and its position on the sheet. As I add a new line of data daily, I shift a bunch of charts down on the page to line up with the new last row. What I DON'T understand is why I need the sheet with the charts on it to be activated in order for this to work properly.

Before anyone asks, my lastrow value is being retrieved explictly, so none of my values are being interpreted from an ActiveSheet. However, I still have this issue... Here is my subroutine that takes some arguments to adjust a specific chart:

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

Here's an example of how I call it:

'Worksheets("main chart").Activate

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

With the Activate line commented out, the charts line up unevenly - note the red line is what I want lining up with my lastrow of data:

But when I un-comment out the Activate line:

It lines up perfectly - What gives??? This is very minor but the functionality of it is driving me nuts. I also have 50 + sheets that are using this code, if I'm running in sequence that's a lot of unnecessary activating...


回答1:


As identified in the question Comments, this is a bug when the sheet is both A) Not Active and B) Not set to 100% Zoom.

The zoom of the sheet that is Active is irrelevant - 100%, 200%, 50%, the same as the sheet with the Chart on: none of that matters.

This following code will take a SheetName as string, and let you change the Zoom without changing ActiveSheet. It will, however, select and unselect sheets.

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



回答2:


Just to mark this question as "solved", this has been confirmed to be a bug - There is no way to have this work without activating or selecting the worksheets. I'm sticking to the solution of using Worksheet().Activate to get my intended result.



来源:https://stackoverflow.com/questions/49493633/code-that-changes-chart-position-requires-my-target-sheet-to-be-activated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!