在userform中选择图表 [英] Select chart within userform

查看:1553
本文介绍了在userform中选择图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写一个宏,它从一个图形复制格式化,并将其应用于多个其他图形。



我所要做的是确定一种方法,允许用户设置模板图表,然后选择多个其他图表。虽然如果用户知道图表名称,我可以使用组合框来完成这项工作,但我想在没有他们知道图表名称的情况下这样做。



具有用户对话框,其中用户可以选择基本图表,然后选择要应用格式化的图表。就像一个范围的 refedit 。但是我不知道如何从用户表单中引用一个图。



可以这样做吗?如果是,怎么办?

解决方案



在用户窗体中放置两个组合框和两个图像控件。



假设您的工作表看起来像这样



>



UserForm_Initialize()事件中填充两个组合框中的图表名称。例如

  Dim ws As Worksheet 

'~~>准备表单
Private Sub UserForm_Initialize()
设置ws = ThisWorkbook.Sheets(Sheet1)

Dim ChartObj As ChartObject

对于每个ChartObj在ActiveSheet.ChartObjects
ComboBox1.AddItem ChartObj.Name
ComboBox2.AddItem ChartObj.Name
Next ChartObj
End Sub

因此,当您运行表单时,它将如下所示:





在组合框的点击事件中,使用Stephen Bullen的PastePicture来自此处的代码,以在用户表单中显示图表。例如

  Private Sub ComboBox1_Click()
ws.Shapes(ComboBox1.Value).CopyPicture
Set Me.Image1.Picture = PastePicture(xlPicture)
End Sub

Private Sub ComboBox2_Click()
ws.Shapes(ComboBox2.Value).CopyPicture
Set Me。 Image2.Picture = PastePicture(xlPicture)
End Sub



从那里开始,现在你有图表的名称。



希望这有助于。




I am looking to write a macro which copies formatting from one graph and applies it to multiple other graphs.

What I am struggling to do is determine a way to allow the user to set the template chart and then select the multiple other charts. While this could be done with a combo box if the user knew the chart name, I am trying to do it without them knowing the chart name.

As such I was thinking of having a user dialog box where the user can select the base chart, and then select the charts to apply the formatting to. Just like refedit for a range. However I cannot figure out how to reference to a graph from within a user form.

Can this be done, and if so, how?

解决方案

Here is what will get you started.

Place Two ComboBoxes and two Image Controls on the userform.

Let's say your worksheet looks like this

In the UserForm_Initialize() event populate the Chart names in both the comboboxes. For example

Dim ws As Worksheet

'~~> Prepare your form
Private Sub UserForm_Initialize()
    Set ws = ThisWorkbook.Sheets("Sheet1")

    Dim ChartObj As ChartObject

    For Each ChartObj In ActiveSheet.ChartObjects
        ComboBox1.AddItem ChartObj.Name
        ComboBox2.AddItem ChartObj.Name
    Next ChartObj
End Sub

So when you run the form, it will look like this

In the click event of the comboboxes, use the Stephen Bullen's PastePicture code from HERE to show the chart in the userform. For example

Private Sub ComboBox1_Click()
    ws.Shapes(ComboBox1.Value).CopyPicture
    Set Me.Image1.Picture = PastePicture(xlPicture)
End Sub

Private Sub ComboBox2_Click()
    ws.Shapes(ComboBox2.Value).CopyPicture
    Set Me.Image2.Picture = PastePicture(xlPicture)
End Sub

This is how the form will look.

From there on, Now you have the names of the charts. Simply use them to work as you please.

Hope this helps.

这篇关于在userform中选择图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆