在VBA中拆分数据

时间:2017-06-12 17:40:59

标签: excel vba excel-vba split

目前我正面临一个问题,特别是我不知道如何解决这个问题。 我有一张桌子:
--------------------------
| d.dasd |
| dasda.dasda |
| fdsfs.fsdf |
| fdsfdsaf.fdsafasdf |
| dasda.dasdasdas |
---------------------------

问题是我想在" dot"之后删除字符串。我在VBA写了一个脚本:

Sub SplitText()

Dim Counter As Integer
Counter = 1
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim Temp As String
Dim Splited As String

Do While Counter <= LastRow

    Temp = Cells(Counter, 1).Value
    Splited = Split(Temp, ".")
    MsgBox Splited

Counter = Counter + 1
Loop

End Sub

但我在“Splited = Split(Temp,&#34;。&#34;)”中遇到了问题。 应删除点后的数据。

我该怎么办?

2 个答案:

答案 0 :(得分:1)

试试这段代码:

Sub SplitText()

    Dim Counter As Integer
    Counter = 1
    Dim i As Integer
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Dim Temp As String
    Dim Splited As Variant  '~~> Splited will be Variant and NOT string

    Do While Counter <= LastRow

        Temp = Cells(Counter, 1).Value
        Splited = Split(Temp, ".")  

        '~~> Run a loop on array and get all the items
        For i = LBound(Splited) To UBound(Splited)
            MsgBox Splited(i)
        Next

        Counter = Counter + 1
    Loop

End Sub

答案 1 :(得分:1)

根据评论,如果您想要的只是.前面的文字,那么只需更改一行:

Sub SplitText()

Dim Counter As Integer
Counter = 1
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim Temp As String
Dim Splited As String

Do While Counter <= LastRow

    Temp = Cells(Counter, 1).Value
    Splited = Split(Temp, ".")(0)
    MsgBox Splited

Counter = Counter + 1
Loop

End Sub

enter image description here