简体   繁体   English

如何使用vba将旋转的形状移动到excel 2010中的特定位置

[英]How to move a rotated shape to a specific location in excel 2010 using vba

I've written some VBA code that automatically creates a chart. 我写了一些自动创建图表的VBA代码。 One of the axes on this chart doesn't use normal labels but a graphic. 此图表中的一个轴不使用普通标签,而是使用图形。 I've stored the graphic as an image and I use the .Copy and .Paste methods to get a copy of this image onto the chart. 我将图形存储为图像,然后使用.Copy和.Paste方法将此图像的副本存入图表。

Here is where it gets confusing. 这是令人困惑的地方。 I need to rotate the image to get it aligned with the axis (using the .rotation property). 我需要旋转图像以使其与轴对齐(使用.rotation属性)。 But when I set the .top and .left properties the shape doesn't end up where I would expect. 但是当我设置.top和.left属性时,形状不会达到我期望的结果。 In fact setting the properties to 0 and 0 doesn't do what I would expect either. 事实上,将属性设置为0和0并不能达到我的预期。 I've tried changing the order of the way I set the properties on the image object but it only appears in a different (wrong) location. 我已经尝试更改我在图像对象上设置属性的方式的顺序,但它只出现在不同的(错误的)位置。

I'm sure I'm missing some vital aspect of how VBA/Excel is placing the object relative to what I'm setting the top and left properties to. 我确定我错过了VBA / Excel如何放置对象相对于我设置顶部和左侧属性的一些重要方面。 Basically my goal is to make the image on the left side of the chart with the same width as the plot area's height (since the image is rotated I theorize this will make it the same size). 基本上我的目标是使图表左侧的图像具有与绘图区域的高度相同的宽度(因为图像旋转我理论化,这将使其大​​小相同)。

This code does not work: 此代码不起作用:

Sheets(ImageSheet).Shapes("agreement").Copy
c.Chart.Paste
c.Chart.Shapes(1).Rotation=270
c.Chart.Shapes(1).width = c.Chart.PlotArea.height
c.Chart.shapes(1).left = 5
c.Chart.Shapes(1).top = c.Chart.PlotArea.top

I've also tried this code 我也试过这段代码

c.chart.Shapes(1).top = c.chart.PlotArea.top + c.Chart.PlotArea.height

because I thought maybe it was calculating the "top" as the upper-left corner of the image object when it is not rotated (rotating 270 degrees makes this point in a place where it should align with the bottom of the plot area). 因为我想也许它正在计算“顶部”作为图像对象的左上角,当它没有旋转时(旋转270度使得这个点在它应该与绘图区域的底部对齐的地方)。 But that doesn't do what I expected either. 但这并没有达到我的预期。

The image is a skinny rectangle that acts as a label for the axis. 图像是一个瘦的矩形,充当轴的标签。 The chart will end up being laid out like this: http://imgur.com/NrSXR and the axis label image would be something like this http://imgur.com/08EWU 图表最终会像这样布局: http//imgur.com/NrSXR和轴标签图像就像这样http://imgur.com/08EWU

What am I missing here? 我在这里错过了什么?

Is it possible for you to align your chart into a position where the shape could rest align/on a cell? 您是否可以将图表对齐到形状可以在单元格上对齐/放置的位置?

IF YES then here is a suggestion:- 如果是,那么这里有一个建议: -

  • You could position shape into a cell . 您可以将shape定位到cell Then adjust the size to what you need. 然后根据需要调整大小。 And rotate. 并旋转。
  • Then change its bring forward property be shown on the Chart . 然后更改其bring forward属性显示在Chart
  • Next Group Chart and the Shape 下一步集团 ChartShape

PS: I recorded a macro. PS:我录了一个宏。 However it's best if you could show us what your the exact picture (=how your sheeet/chart/image should look like) of your question. 但是,如果您能够向我们展示您的问题的确切图片 (=您的Sheeet /图表/图像应该如何),那将是最好的。

I ended up rotating and resizing the image before copying and pasting to the chart and then positioning it. 在复制并粘贴到图表然后定位之前,我最终旋转并调整图像大小。 I had to use the IncrementLeft and IncrementTop methods rather than setting the left and top properties directly because that did not have the desired effect. 我不得不使用IncrementLeft和IncrementTop方法,而不是直接设置left和top属性,因为它没有达到预期的效果。

When doing the paste into the chart the object always ended up in the upper left hand-corner so I could increment to the left by the small amount I wanted as a margin I wanted there and increment the top by the value of PlotArea.top to align it with the plot area. 当粘贴到图表中时,对象总是在左上角结束,所以我可以向左增加我想要的少量我想要的边距并将顶部增加PlotArea.top的值到将其与绘图区域对齐。

I was also surprised that when creating the copy of my image it retained the "name" i referred to it as when I copied it to the new sheet and chart. 我还惊讶的是,在创建我的图像副本时,它保留了“名称”,我将其称为当我将其复制到新的图纸和图表时。 This was especially useful for positioning the image once it was on the chart. 这对于在图像上定位图像时特别有用。

I also needed execute this code at the very end of my procedure, after everything else had been positioned and aligned, or when I positioned the data labels for one of my series they wouldn't appear correctly. 我还需要在我的程序结束时执行此代码,在其他所有内容都已定位并对齐之后,或者当我为其中一个系列定位数据标签时,它们将无法正确显示。

Here is the code that I ended up using: 这是我最终使用的代码:

    'make a copy of the label image and refer to it with a variable for convenience
    Sheets(ImageSheet).Shapes("maturity").Copy
    i = Sheets(ImageSheet).Shapes.Count
    Sheets(ImageSheet).Paste
    Dim axisImage As Shape
    Set axisImage = Sheets(ImageSheet).Shapes(i + 1)

    'rotate and resize the image
    With axisImage
        .Rotation = 270
        .width = c.Chart.PlotArea.height
    End With

    'cut and paste the new image to the chart
    axisImage.Cut
    c.Chart.Paste

    'position it in the chart using IncrementTop and IncrementLeft because setting the properties directly does not have the desired effect
    c.Chart.Shapes("maturity").IncrementTop c.Chart.PlotArea.top
    c.Chart.Shapes("maturity").IncrementLeft c.Chart.PlotArea.left - c.Chart.Shapes("maturity").height

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

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