excel中的数组更改了字符串的格式

时间:2016-06-22 15:21:55

标签: excel vba excel-vba

enter image description here

我想要将图像中的数据分开。一旦到达分号和缩进,图像中的代码就不会分开。我想把这些单词分成一个新的表格,所以我要在数组中搜索某些单词。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

以下代码适用于大小为100的数组,您可以将其修改为更高的值,或使用带有REDIM的动态数组。

Sub find()

Dim s As String
Dim Data(100) As Variant
Dim i As Integer

For i = 1 To Range("A1").End(xlDown).Row
    Cells(i, 1).Font.Underline = True
    Cells(i, 2).Value = FindWord(Cells(i, 1), 2) ' change 2 to whatever word position in the original string you want to copy to the right column
Next i

End Sub

'我添加了这个功能

Function FindWord(Source As String, Position As Integer)

Dim cell_strarr() As String
cell_strarr = Split(Source, " ")
strWordCount = UBound(cell_strarr)
If strWordCount < 1 Or (Position - 1) > strWordCount Or Position < 0 Then
    FindWord = ""
Else
    FindWord = cell_strarr(Position - 1)
End If

End Function

答案 1 :(得分:1)

以下代码假设您在A列中有数据。它会将值放在B,C等列中......

Sub find()

    Dim s As String
    Dim Data As Variant
    Dim i As Integer

    NumRows = ActiveSheet.Range("A1048576").End(xlUp).Row
    s = ActiveCell.Value
    Data = Split(s, " ")

    For i = 0 To NumRows
        Data = Split(Cells(i + 1, 1), " ")
        x = 2
        For j = 0 To UBound(Data)
            Cells(i + 1, x).Value = Data(j)
            x = x + 1
        Next j
    Next i

End Sub

答案 2 :(得分:1)

以下代码不仅会拆分数据,还会复制格式化,这似乎也是您想要的。假设数据在A列中

Option Explicit
Sub SplitWithFormat()
    Dim R As Range, C As Range
    Dim i As Long, V As Variant

Set R = Range("a1", Cells(Rows.Count, "A").End(xlUp))
For Each C In R
    With C
        .TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _
        consecutivedelimiter:=True, Tab:=False, semicolon:=False, comma:=False, _
        Space:=True, other:=False

        .Copy
        Range(.Offset(0, 1), Cells(.Row, Columns.Count).End(xlToLeft)).PasteSpecial xlPasteFormats
    End With
Next C
Application.CutCopyMode = False

End Sub

enter image description here