[英]Merging a range of excel cells in a column into one single cell seperated by new lines
我需要Excel的幫助。
我有一列包含數百個單元格的列,需要將它們合並成一個單元格。
單元格中的值已居中。 同樣,某些單元格具有多個值,這些值使用( ALT + ENTER )彼此堆疊。
我需要選擇這些單元格的范圍,並將它們組合起來,然后彼此堆疊成一個單元格。
如果我還可以消除這些值之間以及重復值之間的多余“新行”,那將是一個額外的好處。
這是它的外觀和我的目標的圖片。 我一直在嘗試學習vbscript和宏,但這是一個最后期限。 感謝您的幫助。
下面顯示了如何將列中的所有數字組合到VBA Excel中的單個單元格中,這就是我所假定的編碼語言。
我使用兩個過程: 1) columnCombine()
Sub和2)自定義Split()
函數,由Microsoft的Wade Tai提供
鏈接到具有拆分功能的Wade文章: http : //msdn.microsoft.com/zh-cn/library/aa155763%28office.10%29.aspx
columnCombine()
:
Sub columnCombine()
'variables needed:
Dim col As Integer
Dim startRow As Integer
Dim endRow As Integer
Dim firstCell As Range
Dim lastCell As Range
Dim i As Integer
Dim s As Variant
Dim destinationCell As Range
Dim strg As Variant
Dim strgTemp() As String
'enter first and last cells of column of interest in the "A1/A2/A3..." format below:'
Set firstCell = Range("A1") 'this can be what you want
Set lastCell = Range("A3") 'this can be what you want
'enter destination cell in same format as above
Set destinationCell = Range("B1") 'this can be what you want
'get column of interest
col = firstCell.Column
'get start row and end row
startRow = firstCell.Row
endRow = lastCell.Row
'set temp string
strg = ""
For i = startRow To endRow
strgTemp = Split(Worksheets("Sheet1").Cells(i, col).Value)
For Each s In strgTemp
If strg = "" Then
strg = s
Else
strg = strg & vbNewLine & s
End If
Next s
Erase strgTemp
Next i
'add column to string
destinationCell.Value = strg
End Sub
Split()
函數:
Public Function Split(ByVal InputText As String, _
Optional ByVal Delimiter As String) As Variant
' This function splits the sentence in InputText into
' words and returns a string array of the words. Each
' element of the array contains one word.
' This constant contains punctuation and characters
' that should be filtered from the input string.
Const CHARS = "!?,;:""'()[]{}"
Dim strReplacedText As String
Dim intIndex As Integer
' Replace tab characters with space characters.
strReplacedText = Trim(Replace(InputText, _
vbTab, " "))
' Filter all specified characters from the string.
For intIndex = 1 To Len(CHARS)
strReplacedText = Trim(Replace(strReplacedText, _
Mid(CHARS, intIndex, 1), " "))
Next intIndex
' Loop until all consecutive space characters are
' replaced by a single space character.
Do While InStr(strReplacedText, " ")
strReplacedText = Replace(strReplacedText, _
" ", " ")
Loop
' Split the sentence into an array of words and return
' the array. If a delimiter is specified, use it.
'MsgBox "String:" & strReplacedText
If Len(Delimiter) = 0 Then
Split = VBA.Split(strReplacedText)
Else
Split = VBA.Split(strReplacedText, Delimiter)
End If
End Function
*更新:
如果您希望在多個不同的列上使用此方法,以將所有內容移動到一個單元格,請遞歸或以某種重復的方式使用此代碼,例如,編寫一個腳本,該腳本使用columnCombine將要引用的列節組合到一個列中的不同單元格。 然后再次運行該程序(或根據需要運行多次),以便將數據放入一個單元格中。
如果要更改遍歷一列的順序,例如要從A4迭代到A1而不是從A1迭代到A4,只需將For i = startRow To endRow
為For i = endRow To startRow
。 請注意,這不會更改單個單元格(僅整列)中數據的組織順序。 換句話說, {["hello","Hello"],["One"],["Two", "Three"]}
將變成{["Two","Three"],["One"],["hello","Hello"]}
要更改單元格中的順序,您需要更改columnCombine()
的For Each
語句,或手動更改strg
的順序。 兩者都不難。
這是我會做的解決方案:
在當前變量之外添加以下內容:
Dim strg2 As Variant
strg2 = ""
更改此代碼:
For i = startRow To endRow
strgTemp = Split(Worksheets("Sheet1").Cells(i, col).Value)
For Each s In strgTemp
If strg = "" Then
strg = s
Else
strg = strg & vbNewLine & s
End If
Next s
Erase strgTemp
Next i
'add column to string
destinationCell.Value = strg
至:
For i = endRow To startRow
strgTemp = Split(Worksheets("Sheet1").Cells(i, col).Value)
For Each s In strgTemp
If strg = "" Then
strg = s
Else
strg = s & vbNewLine & strg
End If
Next s
If strg2 = "" Then
strg2 = strg
Else
strg2 = strg2 & vbNewLine & strg
End If
strg = ""
Erase strgTemp
Next i
'add column to string
destinationCell.Value = strg2
請記住,此更改特定於向后迭代項目並向后重新排序。 columnCombine()
子將非常取決於您希望數據如何呈現
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.