简体   繁体   English

Excel VBA在复制为图像之前更改图

[英]Excel VBA changes plots before copying as image

I have been using macros to copy multiple Excel charts at once so I can manually paste them into OneNote as part of a reporting process. 我一直在使用宏一次复制多个Excel图表,因此在报告过程中可以将它们手动粘贴到OneNote中。 However, after switching to Office 365 everything has been acting very strange. 但是,切换到Office 365后,一切都变得非常奇怪。

I start by creating all of these charts on their respective sheets and then I have a macro that consolidates all of the charts onto one sheet. 首先,在它们各自的工作表上创建所有这些图表,然后创建一个宏,将所有图表合并到一张工作表中。 This macro creates a new sheet (rpt_sht) and for each chart I want to copy over, it creates a new chart on the rpt_sht page, copies the the original chart and pastes it into the newly created chart on the rpt_sht page. 此宏创建一个新的工作表(rpt_sht),对于要复制的每个图表,它在rpt_sht页上创建一个新图表,复制原始图表并将其粘贴到rpt_sht页上的新创建的图表中。

' create a new chart on the report page
Set sh = rpt_sht.Shapes.AddChart(, rpt_rng.Left, rpt_rng.Top, w, h)
' select and copy the data from the original chart
' Charts are awful so you gotta do all this so it actually copies
wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Activate
wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Select
For Each srs In wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Chart.SeriesCollection
    names.Add (srs.Name)
Next
DoEvents
wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Copy
DoEvents
' and since charts are still awful, do all this awful stuff to paste it
rpt_sht.Activate
rpt_rng.Activate
sh.Select
sh.Chart.Paste
' so the right series names actually show up
For k = names.Count To 1 Step -1
    sh.Chart.SeriesCollection(k).Name = names(k)
    sh.Chart.SeriesCollection(k).MarkerSize = 3
    names.Remove (k)
Next k

I then have a few buttons on the rpt_sht page that allow me to group, copy as bitmap, and ungroup different combinations of these charts. 然后,我在rpt_sht页面上有几个按钮,可用于分组,复制为位图以及取消分组这些图表的不同组合。

With ws.Shapes.Range(names).Group
    .CopyPicture Format:=xlBitmap
    .Ungroup
End With

There are a two problems that arise: 出现两个问题:

1) When copying the charts between sheets, the chart legend changes from using the data series names to using Series 1, Series 2, etc... I was able to fix this by saving the names in a collection before copying and then renaming everything after pasting, but I feel like that shouldn't happen. 1)在工作表之间复制图表时,图表图例从使用数据系列名称更改为使用系列1,系列2等。我能够通过在复制之前将名称保存在集合中然后重新命名所有内容来解决此问题。粘贴后,但我觉得这不应该发生。

2) When I copy between sheets or as a bitmap, the marker sizes randomly change for only some of the series. 2)当我在图纸之间或作为位图进行复制时,标记大小仅在某些系列中随机变化。 I set them all to 3 after pasting in the rpt_sht, but the problem continues when I try to copy as bitmap. 粘贴rpt_sht后,将它们全部设置为3,但是当我尝试复制为位图时,问题仍然存在。

I can't get either of these things to happen when I do the same operations manually. 当我手动执行相同的操作时,这些事情都不会发生。 Really I have been able to fix the copying/pasting between sheets, but now the series change size after copying as bitmap and I am having a harder time to fix that issue. 确实,我已经能够解决工作表之间的复制/粘贴问题,但是现在该系列在作为位图复制后会更改大小,并且我很难解决该问题。

So the question is, why is this happening and how can I avoid it? 所以问题是,为什么会这样?我该如何避免呢?

I was able to fix the sheet-to-sheet issue by copying the chart and pasting it to the sheet instead of a blank chart. 通过复制图表并将其粘贴到工作表而不是空白图表,我能够解决工作表间的问题。 You can't seem to paste to a cell though. 不过,您似乎无法粘贴到单元格。 For positioning, the sheet paste method has a destination argument that I used to specify the position of the top left corner of the chart. 对于定位,工作表粘贴方法具有一个目标参数,我用于指定图表左上角的位置。

wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Activate
wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Select
wbk.Worksheets(shts(i)).ChartObjects(chts(i)).Copy
' paste the chart to the report page in the rpt_rng cell
rpt_sht.Paste rpt_rng
' get access to the chart and set the size
Set co = rpt_sht.ChartObjects(chts(i))
co.Width = w
co.Height = h

After doing this, copying to OneNote has worked as well, so I guess it must have been something weird with creating a new chart and pasting into it. 完成此操作后,复制到OneNote的效果也很好,因此我想创建新图表并将其粘贴到其中一定很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM