[英]How to check the cell value of other workbook is empty or not in excel vba
Dim ConsolidateSheetObj As Workbook
Dim str As String
Dim keycount,row As Interger
Set ConsolidateSheetObj = Workbooks.open("filePath")
Set str = ConsolidateSheetObj.Sheets(3).Cells(row, 17 + keycount).Value
If str.IsEmpty() Then
. . .
...
End If
強制轉換為字符串並檢查其長度是一種糟糕的方法。 此外,您使用的Set str
在語法上無效: Set
僅用於對象類型。
更好的方法是:
Dim v as Variant
v = ConsolidateSheetObj.Sheets(3).Cells(row, 17 + keycount).Value
If VarType(v) = vbEmpty Then
'this is an empty range
End If
Sub checkEmpty()
'Used variables
Dim filePath As String
Dim sheet, row, keycount As Integer
Dim cellEmpty As Boolean
'Disable Screen updates
Application.ScreenUpdating = False
filePath = "C:\Your_file.xlsx"
sheet = 3
row = 3
keycount = 2
cellEmpty = False
'Open other workbook
Workbooks.Open Filename:= _
filePath
'Check if cell is empty
If isEmpty(Sheets(sheet).Cells(row, 17 + keycount)) Then
cellEmpty = True
End If
'Close other workbook
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=No
If cellEmpty Then
MsgBox ("Cell is Empty!")
'...Your Code here!
End If
'Enable Screen updates
Application.ScreenUpdating = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.