下載源檔案 : http://lwl0606.cmszs.com/archives/excel-vba-string-function.html
在Excel 裡面 如果要合并字串可以用函數 =CONCATENATE(A4,B4,C4)
當然也可以 =A4 & B4 & C4
下面的函數可以實現分組合并字串
第一個參數是分組的列,第二個是分組的內容,按照那個分組,第三個參數是同一分組內的排序列,第四個是要合并字串的列
Function GT(GC As Range, G, SC As Range, VC As Range)
Dim vResult
Dim I As Integer
Dim J As Integer
Dim s As Integer
s = 0
Dim t As Integer
t = 1
Dim a() As Integer
Dim b() As String
For I = 1 To GC.Rows.Count
If GC.Item(I, 1) = G Then
s = s + 1
End If
Next I
ReDim a(s) As Integer
ReDim b(s) As String
For I = 1 To GC.Rows.Count
If GC.Item(I, 1) = G Then
a(t) = SC.cells(I, 1)
b(t) = VC.cells(I, 1)
t = t + 1
End If
Next I
For I = 1 To s
For J = I + 1 To s
If a(I) > a(J) Then
temp = b(J)
b(J) = b(I)
b(I) = temp
temp = a(J)
a(J) = a(I)
a(I) = temp
End If
Next J
Next I
For I = 1 To s
vResult = vResult & b(I) & ";"
MsgBox a(0)
Next I
GT = vResult
End Function
=GT(A2:A10,A4,B2:B10,C2:C10) 得到結果aaaa;cccc;bbbb;
| R |
1 |
tr |
| T |
1 |
tt |
| A |
1 |
aaaa |
| A |
9 |
bbbb |
| A |
3 |
cccc |
| b |
1 |
dddd |
| c |
1 |
eee |
| d |
1 |
xxx |
| d |
2 |
yyy |
下載源檔案 : http://lwl0606.cmszs.com/archives/excel-vba-string-function.html