2016-06-24 126 views
1

我在vba中運行此代碼,我想將整個單元格分隔到新工作表中的一個數組中,但我無法將它拆分成單元格中的所有內容,如下圖所示。我想要它分裂成一個新的單元格和一個新的數組,以便我可以搜索該數組中的關鍵詞。請看代碼,看看可以做些什麼。如果你的細胞包裹着CR-LF或只是一個LF單元格中的整個字符串不拆分

感謝

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

 

 
Set R = Range("d1", Cells(Rows.Count, "d").End(xlUp)) 
 
For Each C In R 
 
    With C 
 
     .TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _ 
 
     consecutivedelimiter:=True, Tab:=False, semicolon:=True, 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

+2

嘗試'分裂'與空格作爲分隔符。 – findwindow

+1

我會使用拆分。你將需要翻倍。您將使用vbCRLF作爲分隔符將單元格拆分爲數組,然後使用空格作爲分隔符分隔每個數組項目。 – Kyle

回答

1

無法從你的例子告訴。

假設它只是一個換行符這應該工作:

替換該行

.TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _ 
    consecutivedelimiter:=True, Tab:=False, semicolon:=True, comma:=False, _ 
    Space:=True, other:=False 

這一行

.TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _ 
consecutivedelimiter:=True, Tab:=False, semicolon:=True, comma:=False, _ 
Space:=True, other:=True, Otherchar:=vbLf 

如果它實際上是一個組合 - 然後使用此:

.TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _ 
consecutivedelimiter:=True, Tab:=False, semicolon:=True, comma:=False, _ 
Space:=True, other:=True, Otherchar:=vbCrLf 

編輯 - 變換分裂細胞進入陣列

在頂部

昏暗varHorizArray爲Variant 昏暗RGE添加新的聲明作爲範圍 昏暗intCol作爲整數

.Copy命令之前,添加

Set rge = Selection 
varHorizArray = rge 

使用結果數組的例子y,加到底

' Array returned is two dimensional - 1 row by 7 columns 
    For intCol = LBound(varHorizArray, 2) To UBound(varHorizArray, 2) 
Debug.Print varHorizArray(1, intCol) 
    Next intCol 
+0

感謝新的工作。我將如何更改偏移量以將數組發送到新工作表,以便我可以搜索內部的字符串? – johndoe253

+0

什麼數組?我看不到任何引用數組的代碼? – dbmitch

+0

如何將分割單元格變成數組? – johndoe253

相關問題