簡體   English   中英

如何在excel vba中檢查其他工作簿的單元格值是否為空

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM