Excel Split()将数字转换为Date

时间:2014-10-09 18:05:47

标签: excel-vba vba excel

我正在尝试实现一个SPLIT()函数来解析EXCEL中的字符串,并将结果分发到同一表单上的相邻单元格。以下是按预期工作,除非解析的术语是数字。

" 0.25"解析为" 1/0/1900 6:00:00 AM"

我对生成的strAttr值尝试了cstr(),但似乎没有效果。有什么想法吗?

 Sub splitText()

 Dim i As Integer
 Dim x As Integer
 Dim strText As String
 Dim strAttr As Variant
 Dim strFirst As String
 Dim NumRows As Integer

    strFirst = "A4"

    Sheets("Data").Activate
    Range(strFirst).Select
    NumRows = Range(strFirst, Range(strFirst).End(xlDown)).Rows.Count

    For x = 1 To NumRows

        ActiveCell.Offset(1, 0).Select

        strText = ActiveCell.Value
        strAttr = Split(strText, " ")

        For i = 0 To UBound(strAttr)
            Cells(x + 4, i + 2).Value = strAttr(i)
        Next i

    Next

End Sub

2 个答案:

答案 0 :(得分:0)

注意:

我强制一个(')到字符串的前面,这似乎提供了所需的结果,除了一切都是= TEXT:

    For i = 0 To UBound(strAttr)
        Cells(x + 4, i + 2).Value = "'" & strAttr(i)
    Next i

有没有更好的方法来实现这一点并将“0.25”保留为数值?

谢谢,

标记

答案 1 :(得分:0)

这可能会有所帮助。替换:

strText = ActiveCell.Value

使用:

strText = ActiveCell.Text