演示Excel VBA中Range是否不为空

时间:2019-07-02 03:44:00

标签: excel vba

如何编写代码以证明从C5到C24的范围不为空,然后将其计入countFile?

Dim FolderPath As String
Dim Path As String
Dim count As Integer
Dim countFile As Integer
Dim Folder As String

FolderPath = Range("B2")
Path = FolderPath & "\*.xls"
Filename = Dir(Path)

If Folder = vbNullString Then
    Range("C2").Interior.ColorIndex = 4
Else
    Range("C2").Interior.ColorIndex = 3
End If

Do While Filename <> ""
   count = count + 1
   Filename = Dir()
Loop

countFile = Application.WorksheetFunction.CountIf(Range("C5:C24") NotIsEmpty)
If count = 6 * countFile - countFile + 1 Then
    Range("C2").Interior.ColorIndex = 4
Else
    Range("C2").Interior.ColorIndex = 3
End If

1 个答案:

答案 0 :(得分:2)

尝试在Excel中有效的公式COUNTIF(C5:C21,"<>")

countFile = Application.WorksheetFunction.CountIf(Range("C5:C24") ,"<>")
相关问题