2012-09-19 96 views
-1

现在我已经做了一个宏,创建两个图表,从不同的工作表取值。 我认为现在代码太多了。我想以某种方式减少它。阵列和许多代码的嵌入

继承人我的代码

Sub AddChart(namn As String, UxV As String, UyV As String, AxV As String, AyV As String, ExV As  String, EyV As String, CA As Integer) 

With ThisWorkbook.Worksheets("Chart").ChartObjects.Add(200, 200, 600, 400).Chart 
    .Parent.Name = namn 

    If Not .HasTitle Then 
     .HasTitle = True 
     .ChartTitle.Text = namn 
    End If 

    .ChartType = xlXYScatterSmoothNoMarkers 
    .Axes(xlValue).CrossesAt = CA 
    .Axes(xlCategory).TickLabels.NumberFormat = "YYYY-MM-DD" 

    With .SeriesCollection.NewSeries 
     .Name = "Us" 
     .XValues = UxV 
     .Values = UyV 
    End With 

    With .SeriesCollection.NewSeries 
     .Name = "Ai" 
     .XValues = AxV 
     .Values = AyV 
    End With 

    With .SeriesCollection.NewSeries 
     .Name = "Eu" 
     .XValues = ExV 
     .Values = EyV 
    End With 
End With 
End Sub 
-------------------- 

Sub calsub() 


Dim n As String 
Dim CA As Integer 
Dim UxT As String 
Dim UyT As String 
Dim AxT As String 
Dim AyT As String 
Dim ExT As String 
Dim EyT As String 

Dim n2 As String 
Dim CA2 As Integer 
Dim UxP As String 
Dim UyP As String 
Dim AxP As String 
Dim AyP As String 
Dim ExP As String 
Dim EyP As String 

n = "Temperature" 
SxT = "=US!A2:A372" 
SyT = "=US!C2:C370" 
NxT = "=AI!A2:A472" 
NyT = "=AI!C2:C472" 
FxT = "=EU!A2:A572" 
FyT = "=EU!C2:C572" 
CA = -20 

n2 = "Precipitation" 
SxP = "=US!A2:A372" 
SyP = "=US!D2:D372" 
NxP = "=AI!A2:A371" 
NyP = "=AI!D2:D371" 
FxP = "" 
FyP = "" 
CA = -100 

Call AddChart(n, UxT, UyT, AxT, AyT, ExT, EyT, CA) 
Call AddChart(n2, UxP, UyP, AxP, AyP, ExP, EyP, CA) 

End Sub 

我在考虑去创造它像下面莫名其妙

Sub AddChart(namn As String, xV() As String, yV() As String, CA As Integer) <------ 

With ThisWorkbook.Worksheets("Chart").ChartObjects.Add(200, 200, 600, 400).Chart 
    .Parent.Name = namn 

    If Not .HasTitle Then 
     .HasTitle = True 
     .ChartTitle.Text = namn 
    End If 

    .ChartType = xlXYScatterSmoothNoMarkers 
    .Axes(xlValue).CrossesAt = CA 
    .Axes(xlCategory).TickLabels.NumberFormat = "YYYY-MM-DD" 

for -----------> 

    With .SeriesCollection.NewSeries 
     .Name = "Us" 
     .XValues = UxV 
     .Values = UyV 
    End With 

next <----------- 

End With 
End Sub 
+0

喜并欢迎来到SO。恐怕这不是SO设计的问题类型。你需要阅读[faq](http://stackoverflow.com/faq),并将其分解为更小的问题。 –

回答

1

好,如果你的范围保持不变,你可以使用类似

For each wks in activeworkbook.worksheets 

或组成一组相关的工作表来循环。

那么你将有`请将.Name = “我们””的

.Name = wksArray(i).name.Name = wks.name istead

您的数组也可以是多维的,所以你可以编写类似:

.Name = array(i,1).name 
    .XValues = array(i,2) 
    .Values = array(i,3) 
+0

对不起,我有点困惑..你可以指定它多一点? – ImNew

+2

dito - 你困惑什么?!尝试查找有关多维数组和工作表对象的一些信息 – Jook

+0

对不起!我昨天是如此分层,所以我感到迷惑。但我确实已经解决了你的问题。 – ImNew