为什么我在 VBA 中的数组没有填充所有值?
Why is my array in VBA not populating all of the values?
这是我的第一个 post。我已经使用 VBA 一个月了,我正在尝试根据用户定义的范围用日期填充数组。例如,用户将输入:05/01/2001 - 05/21/2001。因为我试图用从开始到结束的所有日子填充一个数组,在这个例子中它将是 21 个日期。当我打印出数组时,我只得到奇数天,而不是偶数天。有人能帮忙吗?谢谢!
我正在使用 DateDiff() 函数来获取开始日期和结束日期之间的天数,以确定我必须包含在数组中的日期数。
temp_csv_file_count是数组中值的数量,input_start_date和input_end_date是字符串,忽略状态,这与其他事情有关。
temp_csv_file_count = DateDiff("d", input_start_date, input_end_date)
temp_csv_file_count = temp_csv_file_count + 1
Dim temp_date() As String
ReDim temp_date(0 To temp_csv_file_count) As String
Dim i As Integer
For i = 0 To temp_csv_file_count
temp_date(i) = DateAdd("d", i, input_start_date)
i = i + 1
Next i
msg = "File Count: " & temp_csv_file_count & ", State: " & temp_state
MsgBox msg
Dim array_contents As String
Dim j As Integer
For j = 0 To temp_csv_file_count
array_contents = array_contents + temp_date(j) + vbNewLine
Next j
MsgBox "the values of my dynamic array are: " & vbNewLine & array_contents
实际:
2001 年 5 月 1 日,
2001 年 5 月 3 日,
2001 年 5 月 5 日,
2001 年 5 月 7 日,
2001 年 5 月 9 日,
2001 年 5 月 11 日,
2001 年 5 月 13 日,
2001 年 5 月 15 日,
2001 年 5 月 17 日,
2001 年 5 月 19 日,
2001 年 5 月 21 日
For i = 0 To temp_csv_file_count
temp_date(i) = DateAdd("d", i, input_start_date)
'i = i + 1 'THIS IS WHY
Next i
for 循环将一次迭代 1,除非在 Step
中指定(您没有列出步骤,因此它假设为 1),您告诉它在循环本身之前加 1迭代(通过 Next i
)。
For i = 0 To temp_csv_file_count Step 1 'added the step to ensure it is understood
temp_date(i) = DateAdd("d", i, input_start_date)
Next i
For-each loop
每次将 i
的值自行增加一(如果您不更改它)。没有理由使用 i = i + 1
。
更多详情:
- 如果您想将
i
的值增加两倍,您可以使用Step 2
:
示例:
For i = 0 To temp_csv_file_count Step 2
temp_date(i) = DateAdd("d", i, input_start_date)
Next i
- 如果要开始循环从下到上ORif循环旨在删除:
示例:
For i = temp_csv_file_count To 0 Step -1
temp_date(i) = DateAdd("d", i, input_start_date)
Next i
这是我的第一个 post。我已经使用 VBA 一个月了,我正在尝试根据用户定义的范围用日期填充数组。例如,用户将输入:05/01/2001 - 05/21/2001。因为我试图用从开始到结束的所有日子填充一个数组,在这个例子中它将是 21 个日期。当我打印出数组时,我只得到奇数天,而不是偶数天。有人能帮忙吗?谢谢!
我正在使用 DateDiff() 函数来获取开始日期和结束日期之间的天数,以确定我必须包含在数组中的日期数。
temp_csv_file_count是数组中值的数量,input_start_date和input_end_date是字符串,忽略状态,这与其他事情有关。
temp_csv_file_count = DateDiff("d", input_start_date, input_end_date)
temp_csv_file_count = temp_csv_file_count + 1
Dim temp_date() As String
ReDim temp_date(0 To temp_csv_file_count) As String
Dim i As Integer
For i = 0 To temp_csv_file_count
temp_date(i) = DateAdd("d", i, input_start_date)
i = i + 1
Next i
msg = "File Count: " & temp_csv_file_count & ", State: " & temp_state
MsgBox msg
Dim array_contents As String
Dim j As Integer
For j = 0 To temp_csv_file_count
array_contents = array_contents + temp_date(j) + vbNewLine
Next j
MsgBox "the values of my dynamic array are: " & vbNewLine & array_contents
实际: 2001 年 5 月 1 日, 2001 年 5 月 3 日, 2001 年 5 月 5 日, 2001 年 5 月 7 日, 2001 年 5 月 9 日, 2001 年 5 月 11 日, 2001 年 5 月 13 日, 2001 年 5 月 15 日, 2001 年 5 月 17 日, 2001 年 5 月 19 日, 2001 年 5 月 21 日
For i = 0 To temp_csv_file_count
temp_date(i) = DateAdd("d", i, input_start_date)
'i = i + 1 'THIS IS WHY
Next i
for 循环将一次迭代 1,除非在 Step
中指定(您没有列出步骤,因此它假设为 1),您告诉它在循环本身之前加 1迭代(通过 Next i
)。
For i = 0 To temp_csv_file_count Step 1 'added the step to ensure it is understood
temp_date(i) = DateAdd("d", i, input_start_date)
Next i
For-each loop
每次将 i
的值自行增加一(如果您不更改它)。没有理由使用 i = i + 1
。
更多详情:
- 如果您想将
i
的值增加两倍,您可以使用Step 2
:
示例:
For i = 0 To temp_csv_file_count Step 2
temp_date(i) = DateAdd("d", i, input_start_date)
Next i
- 如果要开始循环从下到上ORif循环旨在删除:
示例:
For i = temp_csv_file_count To 0 Step -1
temp_date(i) = DateAdd("d", i, input_start_date)
Next i