将字符串转换为单个字符

3

我的问题可能听起来很愚蠢,但我尝试了网上提供的几种解决方案,没有任何成功。 我对VBA完全不熟悉。

问题: 我正在从txt文件中提取数据。将数据存储到字符串中,然后我需要将该值存储到单个变量中。

背景: 我正在使用Microsoft Excel 2010,在Microsoft Visual Basic for Applications下编写代码。

尝试过的解决方案: 在所有这些情况下,valueToRead等于"1.580000"。

1) realAngle = CSng(valueToRead) 我得到执行错误'13',类型不兼容

2) realAngle = Single.Parse(valueToRead) 编译错误,语法错误

3) realAngle = Single.Parse(valueToRead, System.Globalization.NumberFormatInfo.InvariantInfo) 编译错误,语法错误

4) realAngle = Convert.ToSingle(valueToRead) 执行错误'424':对象必需

我的代码如下:

Sub macro_test()
' File to read data from
Dim logFile As String
' Variable containing a line from the file
Dim textLine As String
' Variable containing a part of the line
Dim ReadValue As Variant
' Variable containing a  part of ReadValue
Dim ReadValue2 As Variant
' Desperate variable in order to be sure to have a String value
Dim valueToRead As String
' Angle value stored in the file
Dim realAngle As Single
' Number of elements separated by " | " in the lien
Dim Size As Integer

' Initialize variables
Size = 0
realAngle = 0
logFile = "FilePathAndName"

' Open the txt file
Open logFile For Input As #1

' Read until the end of the file
Do Until EOF(1)
    ' Get a line of text from the file
    Line Input #1, textLine
    ' Split the line with " | " separator
    ReadValue = Split(textLine, " | ")

    ' Count the number of elements
    Size = UBound(ReadValue) - LBound(ReadValue) + 1

    ' If the line have enough elements then it may be of interest
    If Size > 9 Then
        ' if this is the correct line thanks to a correct identificator
        If ReadValue(3) = "MyIdentificator" Then
        ' Split the line with the " = " sign
        ReadValue2 = Split(ReadValue(8), " = ")
        ' Storing the value into a String
        valueToRead = ReadValue2(1)

        realAngle = CSng(valueToRead)
        'realAngle = Single.Parse(valueToRead)
        'realAngle = Convert.ToSingle(valueToRead)
        'realAngle = Single.Parse(valueToRead, System.Globalization.NumberFormatInfo.InvariantInfo)
        End If
    End If
Loop

Close #1
End Sub

感谢您的帮助!以下是您需要翻译的内容:

编辑:以下是我在文件中得到的一行示例: 日志级别:LogLevel | 文件名:X:\Folder1\Folder2\Folder3\Folder4\Folder5\Folder6\FileName.h | 函数名:FunctionName| 行号:XXXX | 信息:MYINFO | BLABLA | VALUE1 = 32768 | VALUE2 = 0.000000 | VALUE3 = 1.580000 | VALUE4 = 0.000000 | VALUE5 = 3581.941895 | VALUE6 = 36349.941406

我正在尝试获取VALUE3。


2
强烈建议使用Double而不是Single。 - Gary's Student
即使值只在-32768和32768之间? - ZulKaz
你需要使用断点并检查你认为是数字的字符串的值。变量valueToRead中可能有字母字符。在导致错误的行之前直接尝试“debug.print valueToRead”。 - John Muggins
支持在VBA中不使用“Single”的想法。 - Vityata
1
你的区域设置是什么?你的小数点是 . 还是 , ? - Tim Williams
显示剩余5条评论
1个回答

1

我猜想你不在美国/英国。在德国,你使用的是,而不是.作为区域设置。因此,在强制转换之前,请将其添加到字符串中:

Public Function change_commas(ByVal myValue As Variant) As String

    Dim str_temp As String

    str_temp = CStr(myValue)
    change_commas = Replace(str_temp, ",", ".")

End Function

我现在明白问题所在了!谢谢大家的快速回答 :) 另外,能否知道为什么Double比Single更可取呢? - ZulKaz
@Zulkaz - 在谷歌上搜索“为什么在VBA中应该使用长整型而不是整型”。原因基本相同。抱歉,我现在离开电脑了。 - Vityata

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接