【问题标题】:How to plot a chart with known column index in vba如何在vba中绘制具有已知列索引的图表
【发布时间】:2019-10-03 10:46:54
【问题描述】:

我正在尝试创建一个简单的用户表单来绘制选定列的图表。

我目前所做的如下代码所示:

Public Sub UserForm_Initialize()
    Dim Axis As Variant
    Dim Axis1 As Variant

    Axis = Rows(1).Value

    ' here I'm filling the Comboboxes with all the names in the 1st row
    ComboBox1.Column = Axis
    ComboBox2.Column = Axis
End Sub

Public Sub Plot_Click()
    With Range("A1:ZZ1")
        'Here I'm searching for the column index of the selected value in the first Combobox
        Set rFind1 = .Find(What:=ComboBox1.Value, LookAt:=xlWhole, _
                           MatchCase:=False, SearchFormat:=False)
    End With
    Spalte1 = rFind1.Column

    With Range("A1:ZZ1")
       'Here I'm searching for the column index of the selected value in the second Combobox
        Set rFind2 = .Find(What:=ComboBox2.Value, LookAt:=xlWhole, _ 
                           MatchCase:=False, SearchFormat:=False)
    End With
    Spalte2 = rFind2.Column

    'somewhere here, I would like to plot a graph with the selected 
    'column indexes (Spalte1 and Spalte2) 

End Sub

Private Sub Schliessen_Click()
    'here I'm closing the userform
    Unload Me
End Sub

图表应如下所示:

而且在未来,我还会添加更多的组合框,以便选择更多的轴来绘制

感谢每一个答案/帮助和建议

【问题讨论】:

    标签: excel vba indexing graph charts


    【解决方案1】:
    1. 初始化组合框的方式会添加许多空白项,在选择所需值时会变得很麻烦。我会建议只添加必需的列。
    2. 一遍又一遍地搜索选中的列索引也是没有意义的。您可以使用 .ListIndex 属性获取基于 0 的索引值。因此,以防万一,实际的列索引将为 ComboBox1.ListIndex + 1。
    3. 由于您将来可能需要添加更多 ComboBox,我宁愿建议使用两个 ListBox,第一个包含所有列名,第二个将更新为使用添加、删除按钮用作系列的所需列,如下图所示:

    【讨论】:

    • 感谢您的建议@Dhirendra 我将按照您的建议更改 UserFrom。我只是一个初学者:) 但是我的实际问题仍然没有回答如何用几个选定的列绘制图表。如果您能给我一些想法,我将不胜感激。谢谢
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多