2014-09-02 158 views
4

我想連接Excel中的一堆列。我知道我可以手動做:Excel - 連接許多列

=A1&", "&B1&", "&C1(等)

,但我有40列,我正在尋找一種方法來簡化這一過程。

在此先感謝您的幫助!

+1

http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/ – 2014-09-02 16:56:58

回答

3

作爲用戶函數採取range

Public Function ClarkeyCat(ByRef rng As Range) As Variant 

Dim c As Range 
Dim ans As Variant 

For Each c In rng 

If (c.Value <> "") Then 
    ans = IIf(ans = "", "", ans & ",") & c.Value 
End If 

Next 

ClarkeyCat = ans 
End Function 

改變Variant類型,如果你需要(以string,最有可能的)。

使用這樣的:

enter image description here

+1

這可能是最優雅的解決方案。 – 2014-09-02 17:30:49

0

您可以隨時使用Visual Basic For Applications(VBA)。它是Office的微軟語言。以下是您可能要查找的示例,但請嘗試使用Google Machine瞭解有關VBA的更多信息以及如何將此代碼輸入到電子表格中。

Sub ConcatColumns()

Do While ActiveCell <> "" 'Loops until the active cell is blank.

'The "&" must have a space on both sides or it will be 
    'treated as a variable type of long integer. 

    ActiveCell.Offset(0, 1).FormulaR1C1 = _ 
    ActiveCell.Offset(0, -1) & " " & ActiveCell.Offset(0, 0) 

    ActiveCell.Offset(1, 0).Select 

Loop

End Sub

+1

這並沒有解決逗號需要放置在單元格值之間。 – 2014-09-02 17:37:22

+1

它只是將一行中的最後兩個單元格連接在一行之前。 – 2014-09-02 17:43:02

2

我會用VBA這一點。對於每一列你會想要類似的東西(假設值在第1行)

myString = "" 
for i = 1 to 40 
    if i <> 40 then 
    myString = myString & Cells(1, i) & ", " 
    else: 
    myString = myString & Cells(1, i) 
    end if 
next i 

myString將會有你的連接字符串的內容。

1

當串聯一系列單個行或列,你可以使用Application.Transpose避免範圍單杆做循環

此UDF有三個參數

  1. 1D範圍(可以是列或行)
  2. A n可選分隔符(如果沒有entrey,則使用,
  3. 可選項用於指定範圍是否爲一行(對於範圍,請輸入TRUE - 進一步認爲我將更新UDF以自動檢測範圍是row OR column爲主)

注意的是,在其他的答案

代碼

Function ConCat(rng1 As Range, Optional StrDelim As String, Optional bRow As Boolean) As String 
Dim x 
If StrDelim = vbNullString Then StrDelim = "," 
x = Application.Transpose(rng1) 
If bRow Then x = Application.Transpose(x) 
ConCat = Join(x, StrDelim) 
End Function 

在下面

  • 式(D1)的例子是=concat(A1:C1,",",TRUE)
  • E1公式是=concat(E3:E5,", ")

enter image description here

2

讓我發表我的功能了。我也遇到過這個問題。
當我嘗試連接日期,錯誤和空白單元格時,通常會出現我的問題。
所以我嘗試使用下面覆蓋的大多數人:

Function CONCATPLUS(ref_value As Range, Optional delimiter As Variant) As String 
    Dim cel As Range 
    Dim refFormat As String, myvalue As String 

    If ref_value.Cells.Count = 1 Then CONCATPLUS = CVErr(xlErrNA): Exit Function 
    If IsMissing(delimiter) Then delimiter = " " 

    For Each cel In ref_value 
     refFormat = cel.NumberFormat 
     Select Case TypeName(cel.Value) 
     Case "Empty": myvalue = vbNullString 
     Case "Date": myvalue = Format(cel, refFormat) 
     Case "Double" 
      Select Case True 
      Case refFormat = "General": myvalue = cel 
      Case InStr(refFormat, "?/?") > 0: myvalue = cel.Text 
      Case Else: myvalue = Format(cel, refFormat) 
      End Select 
     Case "Error" 
      Select Case True 
      Case cel = CVErr(xlErrDiv0): myvalue = "#DIV/0!" 
      Case cel = CVErr(xlErrNA): myvalue = "#N/A" 
      Case cel = CVErr(xlErrName): myvalue = "#NAME?" 
      Case cel = CVErr(xlErrNull): myvalue = "#NULL!" 
      Case cel = CVErr(xlErrNum): myvalue = "#NUM!" 
      Case cel = CVErr(xlErrRef): myvalue = "#REF!" 
      Case cel = CVErr(xlErrValue): myvalue = "#VALUE!" 
      Case Else: myvalue = "#Error" 
      End Select 
     Case "Currency": myvalue = cel.Text 
     Case Else: myvalue = cel 
     End Select 

     If Len(myvalue) <> 0 Then 
      If CONCATPLUS = "" Then 
       CONCATPLUS = myvalue 
      Else 
       CONCATPLUS = CONCATPLUS & delimiter & myvalue 
      End If 
     End If 
    Next 
End Function 

截至目前,我還沒有遇到一個單元格輸入這個功能不能連接。
隨意適應您的需求或心中的內容。 HTH。