我正在尝试使用下面的代码,但是我不明白为什么它会打印出空白的消息框?另外,每天只有一个文件,并且说有2个文件?
如何有效地将这些纸打印回来,其次,我该如何使用它来打开纸页?
文件写为samadmin15112018_??????.csv
,问号是我不知道的时间戳。
Sub runFA()
Const yourfilepath = "R:\samsdrive\sam\test\"
Dim s As String
Dim x As Integer
Dim v() As String
s = Dir(yourfilepath & "samadmin" & format(Sheets("Name").Range("C3"), "yyyymmdd") & "_*.csv")
v = Split(vbNullString)
Do Until s = ""
x = x + 1
ReDim Preserve v(x + 1)
s = Dir()
Loop
If UBound(v) > 0 Then
MsgBox "There are " & UBound(v) & " workbooks", vbOKOnly
MsgBox v(x + 1)
Else
If v(0) <> "" Then Workbooks.Open (yourfilepath & v(0))
MsgBox ("There are 0 ")
End If
End Sub
答案 0 :(得分:0)
修复the previous answer ...
您得到一个空元素,因为原始代码调整了第一个元素的数组大小,这意味着v(0)
总是 1}}。对于字符串数组,如果要向其中添加元素,则可以利用vbNullString
函数的行为,返回Split
为-1和UBound
为0的数组动态地:
LBound
还有两点需要注意
Sub runFA()
Const targetPath = "R:\samsdrive\sam\test\"
Dim located() As String
located = Split(vbNullString)
Dim result As String
result = Dir$(targetPath & "samadmin" & Format$(Sheets("Name").Range("C3"), "yyyymmdd") & "_*.csv")
Do Until result = vbNullString
ReDim Preserve located(UBound(located) + 1)
located(UBound(located)) = result
result = Dir$()
Loop
If UBound(located) <> 0 Then
MsgBox "There are " & (UBound(located) + 1) & " workbooks", vbOKOnly
Else
Workbooks.Open targetPath & result
End If
End Sub
和Dir
使用字符串类型的函数。Format
来跟踪结果计数。x
-没有任何理由索引回该数组。