如何在VBA宏中检查空数组 [英] How to check for empty array in vba macro

查看:262
本文介绍了如何在VBA宏中检查空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查空数组. Google给了我各种各样的解决方案,但是没有任何效果.也许我没有正确应用它们.

I want to check for empty arrays. Google gave me varied solutions but nothing worked. Maybe I am not applying them correctly.

Function GetBoiler(ByVal sFile As String) As String
'Email Signature
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.ReadAll
    ts.Close
End Function

Dim FileNamesList As Variant, i As Integer
' activate the desired startfolder for the filesearch
FileNamesList = CreateFileList("*.*", False) ' Returns File names
' performs the filesearch, includes any subfolders
' present the result
' If there are Signatures then populate SigString
Range("A:A").ClearContents
For i = 1 To UBound(FileNamesList)
    Cells(i + 1, 1).Formula = FileNamesList(i)
Next i

SigString = FileNamesList(3)

If Dir(SigString) <> "" Then
    Signature = GetBoiler(SigString)
Else
    Signature = ""
End If

如果FileNamesList数组为空,则完全不应该调用GetBoiler(SigString).当FileNamesList数组为空时,SigString也为空,这将使用空字符串调用GetBoiler()函数.我在

Here if FileNamesList array is empty, GetBoiler(SigString) should not get called at all. When FileNamesList array is empty, SigString is also empty and this calls GetBoiler() function with empty string. I get an error at line

Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)

,因为sFile为空.有什么方法可以避免这种情况?

since sFile is empty. Any way to avoid that?

推荐答案

在处理字符串数组时,您是否考虑过Join?

As you are dealing with a string array, have you considered Join?

If Len(Join(FileNamesList)) > 0 Then

这篇关于如何在VBA宏中检查空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆