簡體   English   中英

對於確定范圍數據類型的VBA公式,忽略范圍中的空白單元格

[英]Ignore blank cells in a Range for a VBA formula that determines Range Data Type

我正在嘗試通讀工作表上的數百列,並確定數據類型是字母,數字還是字母數字。 但是,當列中的空單元格過多時,該公式將繼續返回“空”。 關於如何最好地忽略范圍內的空單元格的任何建議?

下面是代碼

Function DataType(aVal As Variant) As String

Select Case TypeName(aVal)
    Case "String"
        If IsNumeric(aVal) Then
            If LCase(aVal) Like "*d*" Then
                DataType = "Alphanumeric"
            Else
                DataType = "numerical"
            End If
        Else
            If aVal Like "*[0-9]*" Then
                DataType = "Alphanumeric"
            Else
                If aVal = vbNullString Then
                    DataType = "null"
                Else
                    DataType = "Alpha"
                End If
            End If
        End If

    Case "Boolean"
        DataType = LCase(TypeName(aVal))

    Case "Double", "Single", "Integer", "Long", "Byte"
        DataType = "Numerical"

    Case "Range"
        DataType = DataType(aVal.Cells(1, 1).Value)

    Case Else
        DataType = LCase(TypeName(aVal))

End Select

End Function

首先檢查您的單元格是否包含某些東西。 您可以使用以下代碼:

Function DataType(aVal As Variant) As String
    If aVal <> "" Then
        'Put the rest of your function code here
    End If
End Function

這樣的事情應該做。

Case Else
    If Not IsEmpty(aVal) Then
        DataType = LCase(TypeName(aVal))
    End If

或者,您可以檢查單元格是否與調用代碼中的.SpecialCells(xlCellTypeBlanks)相交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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