Ponieważ z tego co napisałaś nie mogę wszystkiego wywnioskować.
Nie wiem czy chcesz dodać wykres do kontrolki , czy tylko zmienić dane...
Załóżmy wobec tego tak:
- na UserForm jest kontrolka SpreedSheet1 (ma 2 arkusze)
- na UserForm kontrolka ChartSpace1
Trochę tego dużo i można uprościć, zwłaszcza, jeżeli chodzi Ci tylko o zmianę danych...
Lecz jak słowo nie wiem do końca co ma być.
A można wiedzieć dlaczego to nie może być normalny ludzki wykres na arkuszu tylko taka kombinajcja "alpejska" ?
Sub ZmienZrodlo(RngZrodlo As Range, _
Optional bDodaj As Boolean = False, _
Optional lAkuszKontrolki As Long = 1)
' zalozmy ze na kontrolce bedzie ten sam adres bezwzgl. dla X i Y co na zrodle czyli arkuszu
Dim strAdres As String
Dim strAddressX As String
Dim strAddressY As String
strAdres = RngZrodlo.Address
With RngZrodlo
strAddressX = .Columns(1).Address
strAddressY = .Columns(2).Address
End With
With Me
With .Spreadsheet1.Sheets(lAkuszKontrolki)
.Activate
.Range(strAdres) = RngZrodlo.Value
End With
With .ChartSpace1
Set .DataSource = Me.Spreadsheet1
If (.Charts.Count > 0 And bDodaj = False) Then
With .Charts(0)
.Type = chChartTypeLineMarkers
.SetData chDimCategories, 0, strAddressX
If .SeriesCollection.Count = 0 Then
.SeriesCollection.Add
End If
.SeriesCollection(1).SetData chDimValues, 0, strAddressY
.HasLegend = True
.HasTitle = True
.Title.Caption = "jakis"
End With
Else
' bo tak naprawde nie wiem czy chcialas dodac czy zmienic
With .Charts.Add
.Type = chChartTypeLineMarkers
.SetData chDimCategories, 0, strAddressX
If .SeriesCollection.Count = 0 Then
.SeriesCollection.Add
End If
.SeriesCollection(0).SetData chDimValues, 0, strAddressY
.HasLegend = True
.HasTitle = True
.Title.Caption = "jakis"
End With
End If
' .Spreadsheet1.Visible = False ' kontrolke mozna ukryc
.Height = 380
End With
End With
End Sub
Nie dodam wykresu tylko zmienie dane na dane z arkusza "2.2.1." skoroszytu
Przechowam je w akuszu 1 kontrolki SpreedSheet
Call ZmienZrodlo(ThisWorkbook.Worksheets("2.2.1.").Range("A1:B10"))
Dodam wykres skorzystam dane z arkusza "2.2.2." skoroszytu
Przechowam je w akuszu 2 kontrolki SpreedSheet
Call ZmienZrodlo(ThisWorkbook.Worksheets("2.2.2.").Range("A1:B10"), True, 2) |