Excel VBA 分組 合并字串函數

來源:互聯網
上載者:User

下載源檔案 : 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.