Excel VBA 分组 合并字符串函数


下载源文件 : 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
= 0
Dim t As Integer
= 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

posted on 2008-12-10 09:43  lwl0606  阅读(3945)  评论(0编辑  收藏  举报

导航

我要啦免费统计