使用当前函数外部的已定义变量

时间:2014-03-17 15:09:35

标签: excel vba excel-vba

我无法从运行该函数的子函数中识别定义的变量。基本上我要做的是为每种数据类型都有一个宏,其中所有定义的都是6个不同的时间:

Sub F3DataSort()

zone1 = "00:26:00"
zone2 = "00:32:00"
zone3 = "00:38:00"
zone4 = "00:44:00"
zone5 = "00:50:00"
zone6 = "00:56:00"

ColumnSort

DrawGraph

End Sub

还有一个F1datasort,F2datasort等,最多4个.Columnsort需要使用此处定义的值来填充具有适当公式的单元格。暂时可以忽略绘图。

columnort的重要部分如下:

Function ColumnSort()
 'Peak value
    Range("E2").Value = "=MAX(RC[-1]:R[9993]C[-1])"
    'Peak time
    Range("F2").Value = "=INDEX(RC[-3]:R[9998]C[-3],MATCH(RC[-1],RC[-2]:R[9998]C[-2],0))"
    'Milestone 1
    Range("F5").Value = zone1
    'Milestone 1 temp formula
    Range("E5").Value = "=INDEX(R[-3]C[-1]:R[9995]C[-1],MATCH(RC[1],R[-3]C[-2]:R[9995]C[-2],1))"
    'Milestone 2
    Range("F6").Value = zone2
    'Milestone 2 temp formula
    Range("E6").Value = "=INDEX(R[-4]C[-1]:R[9994]C[-1],MATCH(RC[1],R[-4]C[-2]:R[9994]C[-2],1))"
    'Milestone 3
    Range("F7").Value = zone3
    'Milestone 3 temp formula
    Range("E7").Value = "=INDEX(R[-5]C[-1]:R[9993]C[-1],MATCH(RC[1],R[-5]C[-2]:R[9993]C[-2],1))"
    'Milestone 4
    Range("F8").Value = zone4
    'Milestone 4 temp formula
    Range("E8").Value = "=INDEX(R[-6]C[-1]:R[9992]C[-1],MATCH(RC[1],R[-6]C[-2]:R[9992]C[-2],1))"
    'Milestone 5
    Range("F9").Value = zone5
    'Milestone 5 temp formula
    Range("E9").Value = "=INDEX(R[-7]C[-1]:R[9991]C[-1],MATCH(RC[1],R[-7]C[-2]:R[9991]C[-2],1))"
    'Milestone 6
    Range("F10").Value = zone6
    'Milestone 6 temp formula
    Range("E10").Value = "=INDEX(R[-8]C[-1]:R[9990]C[-1],MATCH(RC[1],R[-8]C[-2]:R[9990]C[-2],1))"

End Function

这只是告诉它在zone1,zone2等定义的特定时间查找数据读取的内容。这就是我想要的东西,我只是想简化它,以便as尽可能多的代码只需要写一次。

https://www.dropbox.com/s/13agn2zihmxzwbu/example%20for%20code.jpg

正如你可能已经知道的那样,我是一个完全初学者,并且随着我的进展而甩开它。我已经尝试过寻找解决方案,但没有找到像我这样的情况。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

Sub f3datasort()

Dim z1 As Date
Dim z2 As Date
Dim z3 As Date
Dim z4 As Date
Dim z5 As Date

z1 = "00:26:00"
z2 = "00:32:00"
z3 = "00:38:00"
z4 = "00:44:00"
z5 = "00:50:00"

Columnsort z1, z2, z3, z4, z5

End Sub

Sub Columnsort(ByVal z1 As Date, ByVal z2 As Date, ByVal z3 As Date, ByVal z4 As Date, ByVal z5 As Date)
     Range("E2").Value = "=MAX(RC[-1]:R[9993]C[-1])"
    'Peak time
    Range("F2").Value = "=INDEX(RC[-3]:R[9998]C[-3],MATCH(RC[-1],RC[-2]:R[9998]C[-2],0))"
    'Milestone 1
    Range("F5").Value = zone1
    'Milestone 1 temp formula
    Range("E5").Value = "=INDEX(R[-3]C[-1]:R[9995]C[-1],MATCH(RC[1],R[-3]C[-2]:R[9995]C[-2],1))"
    'Milestone 2
    Range("F6").Value = zone2
    'Milestone 2 temp formula
    Range("E6").Value = "=INDEX(R[-4]C[-1]:R[9994]C[-1],MATCH(RC[1],R[-4]C[-2]:R[9994]C[-2],1))"
    'Milestone 3
    Range("F7").Value = zone3
    'Milestone 3 temp formula
    Range("E7").Value = "=INDEX(R[-5]C[-1]:R[9993]C[-1],MATCH(RC[1],R[-5]C[-2]:R[9993]C[-2],1))"
    'Milestone 4
    Range("F8").Value = zone4
    'Milestone 4 temp formula
    Range("E8").Value = "=INDEX(R[-6]C[-1]:R[9992]C[-1],MATCH(RC[1],R[-6]C[-2]:R[9992]C[-2],1))"
    'Milestone 5
    Range("F9").Value = zone5
    'Milestone 5 temp formula
    Range("E9").Value = "=INDEX(R[-7]C[-1]:R[9991]C[-1],MATCH(RC[1],R[-7]C[-2]:R[9991]C[-2],1))"
    'Milestone 6
    Range("F10").Value = zone6
    'Milestone 6 temp formula
    Range("E10").Value = "=INDEX"
End Sub