简体   繁体   中英

MS VBA, Change graph's data range by user

There are two ListBoxes, one has all of the Automotive Parts's names, the other is which parts has the user selected.

The List Boxes

Each part has the part's name, its production date, and the capacity of the line. The Goal is that once the user selects which parts he/she wants to see how it affects the capacity of the line, when he/she presses the "Make me a Chart" button a bar chart appears with only the parts that he/she selected.

I used two For loops and one If then statement to first for each name selected and for each cell in the range where all the part's names are, it will look for that name, once found, it will create a range which includes that part's name, production date, and capacity. Lastly, it will add that range using the Union command to the graph's range.

However, when the code runs, only the current capacity and the last part name selected appear on the graph (two bars).

Any ideas as to what i'm doing wrong?

Dim actualrange As Range
Dim totalrange As Range
Dim c As Range
Dim workingrange As Range
Dim findingnameo As Long
Set totalrange = Range("M1:DA1")
Dim p As Long
Dim tryingrange As Range
For p = 0 To SelectedListBox.ListCount - 1
  For Each c In totalrange.Cells
        If c.Text = SelectedListBox.List(p) Then

         findingnameo = Sheets("The Master").Range("M1:DA1").Find(c.Value, searchdirection:=xlNext, SearchOrder:=xlByColumns).Column

         Dim thename As Range
         Dim thedate As Range
         Dim thecap As Range

         Set thename = Cells(1, findingnameo)
         Set thedate = Cells(2, findingnameo)
         Set thecap = Cells(3, findingnameo)

          Set workingrange = Union(thename, thedate, thecap)

           Dim currentcap As Range

          Set currentcap = Range("M1:M3")

         Set actualrange = Union(currentcap, workingrange)

End If


Next c


Next p

Thank you!!

Thought it was more complicated than it actually was...

Dim currentcap As Range

          Set currentcap = Range("M1:M3")
            Set actualrange = currentcap

For p = 0 To SelectedListBox.ListCount - 1
  For Each c In totalrange.Cells
        If c.Text = SelectedListBox.List(p) Then

         findingnameo = Sheets("The Master").Range("M1:DA1").Find(c.Value, searchdirection:=xlNext, SearchOrder:=xlByColumns, LookAt:=xlWhole).Column

         Dim thename As Range
         Dim thedate As Range
         Dim thecap As Range

         Set thename = Cells(1, findingnameo)
         Set thedate = Cells(2, findingnameo)
         Set thecap = Cells(3, findingnameo)

          Set workingrange = Union(thename, thedate, thecap)



         Set actualrange = Union(actualrange, workingrange)

End If


Next c


Next p

I just needed to put do

set actualrange= Union(actualrange,workingrange)

instead of

set actualrange = Union(currentcap, workingrange)

You learn by your mistakes right?

Although please share any tips or tricks,I want to learn!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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