简化使用数组填充变量

时间:2014-05-22 11:32:58

标签: arrays vba

我的“问题”是我已经设置了一个数组,该数组是从预算矩阵填充的,预算矩阵可能会逐月波动。我有一个代码可以很好地抓住每个月的预算,但我不禁想到必须有一个更简单的方法来填充月预算变量。

Sub Calculate_CarryOver_To_Date()

'Get budget for each month

Dim Expense_Row As Integer
Dim Expense_Column(12) As Integer

'Set Settings Cross Hairs To 0
Expense_Row = 0
Erase Expense_Column

'Search for Horizontal Cross Hair
Expense_Row = Worksheets("Settings").Cells.Find(What:="materials", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Row

'Error Handle Row To Set Horizontal On
If Expense_Row = 0 Then

No_Budget = True

'Set Error Message
Financial_Report.Frame2.Visible = False

Exit Sub
End If 'End Error Handle

'Search For Vertical Cross Hair
Expense_Column(10) = Worksheets("Settings").Cells.Find(What:="January", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(11) = Worksheets("Settings").Cells.Find(What:="February", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(12) = Worksheets("Settings").Cells.Find(What:="March", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(1) = Worksheets("Settings").Cells.Find(What:="April", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(2) = Worksheets("Settings").Cells.Find(What:="May", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(3) = Worksheets("Settings").Cells.Find(What:="June", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(4) = Worksheets("Settings").Cells.Find(What:="July", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(5) = Worksheets("Settings").Cells.Find(What:="August", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(6) = Worksheets("Settings").Cells.Find(What:="September", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(7) = Worksheets("Settings").Cells.Find(What:="October", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(8) = Worksheets("Settings").Cells.Find(What:="November", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Expense_Column(9) = Worksheets("Settings").Cells.Find(What:="December", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column


'Align Cross Hairs To Get Value
Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(1)).Value
AprA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(2)).Value
MayA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(3)).Value
JunA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(4)).Value
JulA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(5)).Value
AugA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(6)).Value
SepA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(7)).Value
OctA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(8)).Value
NovA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(9)).Value
DecA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(10)).Value
JanA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(11)).Value
FebA = Budget_This_Month

Budget_This_Month = Worksheets("Settings").Cells(Expense_Row, Expense_Column(12)).Value
MarA = Budget_This_Month  

End Sub

有没有办法通过Expense_column进行迭代,并以更快的方式填充每月预算变量?

由于

更新

感谢您的回答,我最终使用的方法是:

MonthSelect(1) = "April"
MonthSelect(2) = "May"
MonthSelect(3) = "June"
MonthSelect(4) = "July"
MonthSelect(5) = "August"
MonthSelect(6) = "September"
MonthSelect(7) = "October"
MonthSelect(8) = "November"
MonthSelect(9) = "December"
MonthSelect(10) = "January"
MonthSelect(11) = "February"
MonthSelect(12) = "March"


For i = 1 To 12
Expense_Column(i) = Worksheets("Settings").Cells.Find(What:=MonthSelect(i), _
SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Column
Budget_Array(i) = Worksheets("Settings").Cells(Expense_Row, Expense_Column(i)).Value
'MsgBox Budget_Array(i) 'For Testing

Next i

然后我可以在Budget_Array(i)上调用该值。让我的生活变得更轻松!谢谢!

1 个答案:

答案 0 :(得分:2)

尝试将其置于for循环中(我不是很熟悉vba所以我无法提供合法的工作vba代码,但我尝试使用手头的知识)并用i替换所有数字1到12。并创建月份的字符串数组

For i = 1 To 13
 Expense_Column(i) = Worksheets("Settings").Cells.Find(What:=yourarrayofmonths(i),
SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole).Colum
Next i

再次使用vba代码,我确定你理解这个概念:)