gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:42:48 29 4
gpt4 key购买 nike

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

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

在这个宏的末尾,我想创建一个图表,其中 x 轴上耗时以及与电报对应的值。

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

  • 十六进制:“A7 C8”
  • bool 值:“$00”或“$01”
  • 百分比:“30 美元”

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

我还无法用十六进制测试它,但转换技巧不适用于百分比/ bool 值

编辑:

首先感谢您的回答。

这是我的最终代码,改编自 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

最佳答案

您需要三个 bool 函数,遵循您的业务逻辑和一些 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", "$01", "$30")
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 = "$00") Or (str = "$01"))
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既是 bool 值又是百分比。在这种情况下,您可以考虑百分比。这是某种映射器,遵循以下业务逻辑:

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

关于VBA 处理多种自定义数据类型的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498118/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com