excel中的数组改变string的格式

在这里输入图像说明

我有我想分开的图像中的数据。 一旦获得分号和缩进,图像中的代码就不会分离。 我想将这些单词分隔到一个新的表格中,所以我将通过数组search某些单词。

谢谢您的帮助!

下面的代码不仅会分割数据,而且还会复制格式,这似乎也是你想要的。 假设数据在列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 

在这里输入图像说明

这下面的代码将假设你有列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 

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

 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

 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