VBA中空字符串的条件

Sha*_*ank 4 excel vba

如何检查vba中的字符串变量是否为空?

如果:

Dim StrFile1 As String, StrFile2 As String
Dim Text3 As String
Dim Len1 as Integer, Len2 As Integer 

  With NewMail
   Text3 = Cells(i, 3).Value
   StrPath = Cells(i, 2).Value & Text3
   Text = Cells(i, 1).Value

  .Subject = 
  ' adds the data in column3 with space as subject
  .From = 
  .To = Text
  .BCC = ""
  .TextBody = 

StrFile1 = Dir(StrPath & "*.txt")
   Len1 = Len(StrFile1)
   Do While Len(StrFile1) > 0
   .AddAttachment StrPath & StrFile1
   StrFile1 = Dir
   Loop

   StrFile2 = Dir(StrPath & "*.pdf")
   Len2 = Len(StrFile2)
   Do While Len(StrFile2) > 0
   .AddAttachment StrPath & StrFile2
   StrFile2 = Dir
   Loop

   If (Len1 & Len2) = 0 Then
   GoTo Last



  '.AddAttachment Text3
  .Send
End With
i = i + 1
Loop

Last:
End With
i = i + 1
Loop
Run Code Online (Sandbox Code Playgroud)

现在我想同时检查 Len1 和 Len2 是否为 0,如果是,那么我想去 Last。

当我使用此代码时,我收到一条消息/编译错误“想要以不带结束” ,我不确定是否

If (Len1 & Len2) = 0 Then
       GoTo Last
Run Code Online (Sandbox Code Playgroud)

这是一个正确的代码。和我需要声明标签 Last 吗??

Abd*_*HAR 6

你有很多方法可以做到这一点,如下所示:

Dim StrFiles As String
StrFiles = Trim(StrFile1 & StrFile2)

If IsEmpty(StrFiles) Then
If StrFiles = vbNullString Then
If StrFiles = "" Then
If StrFiles = Empty Then
If Len(StrFiles) = 0 Then
Run Code Online (Sandbox Code Playgroud)

您可以使用+ operator检查 2 个字符串是否为空引用您的代码,因为Len Function返回一个整数,其中包含字符串中的字符数

If (Len1 + Len2) = 0 Then
Run Code Online (Sandbox Code Playgroud)