Results 1 to 1 of 1

how to populate Listbox on userform

Threaded View

kmsoni how to populate Listbox on... 09-22-2011, 02:13 PM
  1. #1
    Registered User
    Join Date
    03-21-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    22

    how to populate Listbox on userform

    Hello Everyone,

    I am facing a problem with populating the listbox on userform by using with statement. I want to create a dynamic chart n then I want to select different range from listbox to view in the chart. for example if i select revenue from listbox chart should show revenue graph, if i select cost from listbox then chart should show cost graph.

    i tried to use
    sub kl()
    dim range2 as range
    
    with userform.userform1
    
    select case listbox1
    
    case1
    range2 = range("b8:f8")
    end with
    end sub

    but this is not helping me populate listbox and also i cannot make my chart dynamic. i am following below mentioned code to have a customized chart. please help me.

    Sub userchart()
    
    ' set dimensions for all variables
    Dim list1 As ListBox, list2 As ListBox
    Dim initial As ChartObject, chart1 As ChartObject
    Dim charttype As Variant
    Dim subtype As Integer, legendkey As Integer
    Dim range1 As range, range2 As range, range3 As range
    Dim none As String
    Dim chartrange As range
    Dim option1 As OptionButton, option2 As OptionButton
    Dim x As Integer
    Dim chttitle As String
    
    'dialog box gets displayed
    With DialogBox1("chart dialog box")
    Set list1 = .ListBoxes(1)
    Set list2 = .ListBoxes(2)
    list2.RemoveAllItems
    list2.AddItem "None"
    For x = 1 To list1.ListCount
        list2.AddItem list1.List(x)
    Next x
       
    'initialize the option buttons
    
    Set option1 = .OptionButtons(1)
    Set option2 = .OptionButtons(2)
    
    'Set the initial values for the lists
        list1.ListIndex = 1
        list2.ListIndex = 1
    'cancel the subroutine if user hits cancel
    If .Show = False Then
    Exit Sub
    End If
    
    If option1.Value = xlOn Then
        If list1.ListIndex = 8 And (list2.ListIndex <> 9 And list2.ListIndex <> 1) Then
        epserror
        option2.Value = xlOn
    
    ElseIf list1.ListIndex <> 8 And list2.ListIndex = 9 Then
        epserror
        option2.Value = xlOn
    End If
    End If
    
    ' check for invalid combination
    
    If list1.ListIndex = list2.ListIndex - 1 Then
     list2.ListIndex = 1
     End If
    
    'set the ranges for the first variable
    With Worksheets("charts")
        Select Case list1.ListIndex
         Case 1
            Set range2 = .range("b8:f8")
         Case 2
            Set range2 = .range("b9:f9")
         Case 3
            Set range2 = .range("b10:f10")
         Case 4
            Set range2 = .range("b11:f11")
         Case 5
            Set range2 = .range("b12:f12")
         Case 6
            Set range2 = .range("b13:f13")
         Case 7
            Set range2 = .range("b14:f14")
         Case 8
            Set range2 = .range("b15:f15")
        End Select
    
    'set the ranges for the second variable
    
        Select Case list2.ListIndex
            Case 1
                 none = "NONE"
            Case 2
                Set range3 = .range("b8:f8")
            Case 3
                Set range3 = .range("b9:f9")
            Case 4
                Set range3 = .range("b10:f10")
            Case 5
                Set range3 = .range("b11:f11")
            Case 6
                Set range3 = .range("b12:f12")
            Case 7
                Set range3 = .range("b13:f13")
            Case 8
                Set range3 = .range("b14:f14")
            Case 9
                Set range3 = .range("b15:f15")
            
        End Select
        
    End With
    '
    'evaluate the option button selected and set chart type
    
    If option1.Value = xlOn Then
            charttype = xl3DColumn
            subtype = 4
            legendkey = 1
        Else: charttype = xlCombination
        subtype = 2
        legendkey = 1
        End If
    End With
    
    'delete the initial chart on the page
    Set initial = Worksheets("charts").ChartObjects(1)
    initial.Delete
    
    'set the year row as x axis
    Set range1 = Worksheets("charts").range("b7:f7")
    
    'set the input options as y axis
    If none = "NONE" Then
        Set chartrange = Union(range1, range2)
        chttitle = list1.List(list1.ListIndex)
    Else
        Set chartrange = Union(range1, range2, range3)
        chttitle = list1.List(list1.ListIndex) & " and " & list2.List(list2.ListIndex)
    End If
    
    'plot charts
    Set chart1 = Worksheets("charts").ChartObjects.Add(360, 6.75, 235, 240)
    
    chart1.Activate
    ActiveChart.ChartWizard _
        Source:=chartrange, _
        gallery:=charttype, _
        Format:=subtype, _
        PlotBy:=xlRows, _
        CategoryLabels:=1, _
        HasLegend:=legendkey, _
        SeriesLabels:=1, _
        Title:=chttitle
    ActiveChart.PlotArea.Select
    Selection.Top = 21
    Selection.Left = 1
    Selection.Width = 249
    Selection.Height = 192
    'set legend formatting
    
    If legendkey = 1 Then
    ActiveChart.Legend.Select
    Selection.Height = 22
    Selection.Left = 1
    Selection.Top = 220
    Selection.Width = 240
    Selection.Border.LineStyle = xlNone
    End If
    
    ActiveChart.ChartTitle.Select
    Selection.AutoScaleFont = True
    With Selection.Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 10
    End With
    
    ActiveWindow.Visible = False
    Worksheets("charts").range("a3").Select
    
    End Sub
    Last edited by Leith Ross; 09-22-2011 at 02:25 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1