VBA 处理多种自定义数据类型的可能性

VBA Handling multiple custom datatype possibilities

我做了一些研究,没有发现任何类似的问题。

我有一个 VBA 宏,可以导入包含设备发送的电报的 .CSV 文件。

在这个宏的最后,我想创建一个图表,其中 x 轴上经过的时间和电报对应的值。

问题是这个值可以是不同的类型:十六进制、布尔值、整数...而且它们不符合标准 Excel 数字格式,这意味着它们不能用于创建图表。 以下是一些示例(值周围有 " 以显示其开始和结束):

And here is an example of data, with custom time format and boolean value

到目前为止,这是我的相关代码,我在其中尝试转换为自定义类型,然后转换回数字以获得通用数字数据类型:

If wsRes.Range("R1").Value Like "$##" Then
    wsRes.Range("R1:R" & plotLine).NumberFormat = "$##"
    wsRes.Range("R1:R" & plotLine).NumberFormat = General
End If

If wsRes.Range("R1").Value Like "??[ ]??" Then
    Dim valArray(1) As String
    For i = 1 To plotLine Step 1
        valArray = Split(wsRes.Range("R" & i), " ")
        wsRes.Range("R" & i).Value = ToInt32(valArray(0) + valArray(1), 16)
        wsRes.Range("" & i).NumberFormat = General
    Next i
End If

我还不能用 hexa 测试它,但是转换技巧不适用于 percentage/boolean

编辑:

首先感谢您的回答。

这是我为任何感兴趣的人编写的最终代码,改编自 Vityata 的代码。

如果需要,此方法将允许轻松添加其他数据类型。

Sub TestMe()
    Dim RangeData as String
    Set wsRes = ActiveWorkbook.Sheets("Results")

    For i = 1 To plotLine Step 1  'plotLine is the last line on which I have data
        DetectType wsRes.Range("R" & i).Value, i
    Next i

    RangeData = "Q1:R" & plotLine
    CreateGraph RangeData 'Call My sub creating the graph
End Sub



Public Sub DetectType(str As String, i As Integer)

    Select Case True
        Case wsRes.Range("R" & i).Value Like "??[ ]??"
            wsRes.Range("R" & i).Value = HexValue(str)

        Case wsRes.Range("R" & i).Value Like "?##"
            wsRes.Range("R" & i).Value = DecValue(str)

        Case Else
            MsgBox "Unsupported datatype detected : " & str
            End
    End Select

End Sub



Public Function HexValue(str As String) As Long
    Dim valArray(1) As String 'Needed as I have a space in the middle that prevents direct conversion
    valArray(0) = Split(str, " ")(0)
    valArray(1) = Split(str, " ")(1)
    HexValue = CLng("&H" & valArray(0) + valArray(1))
End Function


Public Function DecValue(str As String) As Long
    DecValue = Right(str, 2)
End Function

您需要三个布尔函数,遵循您的业务逻辑和一些 Clean Code principles (although the author of the book does not recognize VBA people as programmers):

  • IsHex()
  • IsBoolean()
  • IsPercentage()

Public Sub TestMe()

    Dim myInput As Variant
    myInput = Array("A7C8", "", "")        
    Dim i As Long        
    For i = LBound(myInput) To UBound(myInput)
        Debug.Print IsHex(myInput(i))
        Debug.Print IsBoolean(myInput(i))
        Debug.Print IsPercentage(myInput(i))
        Debug.Print "-------------"
    Next i        
    'or use this with the DetectType() function below:
    'For i = LBound(myInput) To UBound(myInput)
    '    Debug.Print DetectType(myInput(i))
    'Next i

End Sub

Public Function IsHex(ByVal str As String) As Boolean    
    On Error GoTo IsHex_Error       
    IsHex = (WorksheetFunction.Hex2Dec(str) <> vbNullString)        
    On Error GoTo 0
    Exit Function    
IsHex_Error:    
End Function

Public Function IsBoolean(ByVal str As String) As Boolean
    IsBoolean = CBool((str = "[=10=]") Or (str = ""))
End Function

Public Function IsPercentage(ByVal str As String) As Boolean
    IsPercentage = (Len(str) = 3 And Left(str, 1) = "$" And IsNumeric(Right(str, 2)))
End Function

然后需要一些额外的逻辑,因为$01既是布尔值又是百分比。在这种情况下,您可以将其视为百分比。这是某种映射器,遵循以下业务逻辑:

Public Function DetectType(str) As String

    Select Case True
        Case IsHex(str)
            DetectType = "HEX!"
        Case IsPercentage(str) And IsBoolean(str)
            DetectType = "Boolean!"
        Case IsPercentage(str)
            DetectType = "Percentage!"
        Case Else
            DetectType = "ELSE!"
    End Select

End Function