Popup vindue hvor man kan vælge ark og område til en graf ?
HejJeg skal have lavet et popup vindue i excel hvor jeg kan vælge hvilket ark det skal være og hvilket område(to forskellige kolonner) der skal tegnes en graf ud fra. Jeg har lavet en makro som kan gøre det for et specifik regneark og to definerede kolonner men jeg kan ikke få det til at virke når jeg laver en UserForm med en combobox og to refedit bokse.
Disse kolonner skal faktisk også vendes således at hvis jeg vælger kolonne N først også S så skal grafen have S som x-akse og N som y-akse.
Her er noget af min makro som jeg godt kan få til virke.
Sub Chart()
'
' Chart Macro
'
'
Sheets("TR6_BP3_BP5_122_125_20050408_00").Select
Range("N:N,S:S").Select
Range("S2").Activate
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Sheets("TR6_BP3_BP5_122_125_20050408_00"). _
Range("N1:N9261,S1:S9261"), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).XValues = _
"=TR6_BP3_BP5_122_125_20050408_00!R2C19:R9261C19"
ActiveChart.SeriesCollection(1).Values = _
"=TR6_BP3_BP5_122_125_20050408_00!R2C14:R9261C14"
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.ChartTitle.Select
Selection.Characters.Text = "Capacity test 067L5640 #115" & Chr(10) & " TR6 BP3"
Selection.AutoScaleFont = False
With Selection.Characters(Start:=1, Length:=36).Font
.Name = "Arial"
.FontStyle = "fed"
.Size = 11.5
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With ActiveChart
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Superheat [°K]"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Capacity in Tons refrigeration [R22]"
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
og så videre
Her er noget af den makro som jeg har prøvet men som ikke virker, den stopper efter jeg har indsat ark og kolonner.
Option Explicit
Public Rng1 As Range
Public Rng2 As Range
Sub Testme()
UserForm2.Show
MsgBox Rng1.Address 'Range from RefEdit
MsgBox Rng2.Address 'Range from RefEdit
Call PlotChart
End Sub
Sub PlotChart()
Rng1.Select 'Range from RefEdit inserted in SetSourceData below
Rng2.Select 'Range from RefEdit inserted in SetSourceData below
Rng1 , Rng2.Activate
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Range(Rng2, Rng1), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).XValues = Rng2
ActiveChart.SeriesCollection(1).Values = Rng1
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.ChartTitle.Characters.Text = "Capacity test 067L5640 #115" & Chr(10) & " TR6 BP3"
Selection.AutoScaleFont = False
With ActiveChart.ChartTitle.Characters(Start:=1, Length:=36).Font
.Name = "Arial"
.FontStyle = "fed"
.Size = 11.5
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
With ActiveChart
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Superheat [°K] "
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Capacity in Tons refrigeration [R22]"
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = False
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
og så videre
og her er den tilhørende UserForm
Private Sub RefEdit1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set Rng1 = Range(RefEdit1.Value)
End Sub
Private Sub RefEdit2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set Rng1 = Range(RefEdit2.Value)
End Sub
Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In Worksheets
ComboBox1.AddItem ws.Name
Next
End Sub
Private Sub ComboBox1_Change()
If ComboBox1.ListIndex = -1 Then Exit Sub
Worksheets(ComboBox1.Value).Activate
End Sub
Jeg håber der er nogen som kan hjælpe for jeg er kørt helt fast nu.
