[Help] Ghép dữ liệu từ nhiều cell vào 1 cell duy nhất (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

bugatino

Thành viên chính thức
Tham gia
14/7/10
Bài viết
54
Được thích
3
Xin chào các bạn. Mình muốn ghép dữ liệu của nhiều cell khác nhau vào 1 cell duy nhất. Mình tìm trên mạng có custom function như sau:

Mã:
[COLOR=#00007F][FONT=Courier][I]Function[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] aConcat([/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]ByVal[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] Range, [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Optional[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] Joiner [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]String[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] = ", ") [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]String[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]Dim[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] arr[/I][/FONT][/COLOR]

[COLOR=#333333][FONT=Courier][I]arr = r.Value[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]If[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Count = 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = r.Value[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]ElseIf[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Rows.Count > 1 And r.Columns.Count > 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = "Range error"[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]ElseIf[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Columns.Count = 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = Join(Application.Transpose(arr), Joiner)[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]Else[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = Join(Application.Transpose(Application.Transpose(arr)), Joiner)[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]End[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]If[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]End[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Function[/I][/FONT][/COLOR]

Nhưng nó gộp tất cả giá trị trong range vào 1 cell bao gồm cả những blank cell.
Mình muốn sửa lại để nó chỉ gộp tất cả những cell có giá trị thôi thì làm thế nào?
Mong mọi người giúp đỡ.
 
Xin chào các bạn. Mình muốn ghép dữ liệu của nhiều cell khác nhau vào 1 cell duy nhất. Mình tìm trên mạng có custom function như sau:

Mã:
[COLOR=#00007F][FONT=Courier][I]Function[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] aConcat([/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]ByVal[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] Range, [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Optional[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] Joiner [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]String[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] = ", ") [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]As[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]String[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]Dim[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] arr[/I][/FONT][/COLOR]

[COLOR=#333333][FONT=Courier][I]arr = r.Value[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]If[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Count = 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = r.Value[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]ElseIf[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Rows.Count > 1 And r.Columns.Count > 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = "Range error"[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]ElseIf[/I][/FONT][/COLOR][COLOR=#333333][FONT=Courier][I] r.Columns.Count = 1 [/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Then[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = Join(Application.Transpose(arr), Joiner)[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]Else[/I][/FONT][/COLOR]
[COLOR=#333333][FONT=Courier][I]aConcat = Join(Application.Transpose(Application.Transpose(arr)), Joiner)[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]End[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]If[/I][/FONT][/COLOR]
[COLOR=#00007F][FONT=Courier][I]End[/I][/FONT][/COLOR][COLOR=#00007F][FONT=Courier][I]Function[/I][/FONT][/COLOR]

Nhưng nó gộp tất cả giá trị trong range vào 1 cell bao gồm cả những blank cell.
Mình muốn sửa lại để nó chỉ gộp tất cả những cell có giá trị thôi thì làm thế nào?
Mong mọi người giúp đỡ.

Dùng hàm này thử:
PHP:
Function JoinText(ByVal Sep As String, ByVal IgnoreBlanks As Boolean, ParamArray sArray()) As String
  Dim tmpArr, SubArr, Arr(), Item, n As Long, tmp As String
  On Error Resume Next
  For Each SubArr In sArray
    tmpArr = SubArr
    If TypeName(tmpArr) <> "Variant()" Then
      tmp = IIf(TypeName(tmpArr) = "Error", "", Trim(CStr(tmpArr)))
      If IgnoreBlanks = False Or Len(tmp) Then
        n = n + 1
        ReDim Preserve Arr(1 To n)
        Arr(n) = tmp
      End If
    Else
      For Each Item In tmpArr
        tmp = IIf(TypeName(Item) = "Error", "", Trim(CStr(Item)))
        If IgnoreBlanks = False Or Len(tmp) Then
          n = n + 1
          ReDim Preserve Arr(1 To n)
          Arr(n) = tmp
        End If
      Next
    End If
  Next
  If n Then JoinText = Join(Arr, Sep)
End Function
Cú pháp =JoinText(Dấu phân cách, có lấy chuổi rổng hay không?, mảng cần lấy)
Ví dụ:
- Dữ liệu của bạn tại A1:A10, trong đó có vài cell rổng
- Bạn muốn nối tất cả chúng vào cell B1 và bỏ qua cell rổng, bạn gõ vào B1 công thức
Mã:
JoinText(", ", TRUE, A1:A10)
Xong!
-----------
Xin nói thêm: Hàm này không chỉ hoạt động trên Range mà còn cho phép thu gôm giá trị từ mảng do công thức trả về đấy. Ngoài ra nó còn cho phép bạn thu gôm từ các vùng nằm không liền kề
 
Web KT

Bài viết mới nhất

Back
Top Bottom