Excel 如何在vba中绘制具有已知列索引的图表

Excel 如何在vba中绘制具有已知列索引的图表,excel,vba,indexing,graph,charts,Excel,Vba,Indexing,Graph,Charts,我试图创建一个简单的用户表单来绘制选定列的图形 到目前为止,我所做的工作如下面的代码所示: 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 =

我试图创建一个简单的用户表单来绘制选定列的图形

到目前为止,我所做的工作如下面的代码所示:

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


图表应如下所示:

在将来,我还将添加更多的组合框,以便选择更多的轴进行打印

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

  • 这样,您在初始化组合框时会添加许多空白项,在选择所需值时会变得很麻烦。我建议只添加必需的列
  • 反复搜索选定的列索引也没有意义。您可以使用
    .ListIndex
    属性获取基于0的索引值。因此,在这种情况下,实际的列索引将是ComboBox1.ListIndex+1
  • 由于您将来可能需要添加更多的组合框,我建议使用两个列表框,第一个列表框包含所有列名,第二个列表框将使用“添加、删除”按钮更新为“需要列作为系列使用”,如下屏幕截图所示:

  • 谢谢您的建议@Dhirendra我将按照您的建议更改用户。我只是一个初学者:)但我的实际问题是,如何用几个选定的列绘制一个图形仍然没有得到回答。如果你能给我一些建议,我将不胜感激。谢谢
    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