Nối dữ liệu các ô trong vùng cùng điều kiện cho trước (1 người xem)

Liên hệ QC

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

ginkgo36

Thành viên mới
Tham gia
13/12/12
Bài viết
38
Được thích
7
Chào mọi người.
Nhờ mọi người giúp em 1 hàm để group những lines có cùng điều kiện lại với nhau. Ví dụ như trong file excel e đính kèm.

- Trong file đính kèm thêm hàm JoinText. Hàm này dùng để nối các chuối lại với nhau, như ví dụ ở cột A, B, C, E. A,B,C là các cột chứa các words cột E là cột kết quả khi dùng hàm JoinText. Cột "Note-Công thức" là hướng dẫn cách chạy hàm JoinText. Tham số truyền của JoinText(delimiter,TRUE, vùng_chứa_các_words_cần_ghép)

- Cột I, J, K mô tả vấn đề của em. STT và word là 2 cột input

Nếu STT giống nhau thì ghép các giá trị trong cột "Word" lại với nhau.

-Cột result là output mà e muốn ra.

Thanks mọi người.

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)
JoinText = Replace(JoinText, "; ", ";")
JoinText = Replace(JoinText, " ;", ";")
JoinText = Replace(JoinText, ",", ";")
JoinText = Trim(JoinText)
End Function
 

File đính kèm

Cho em đào mộ xíu.
Với đoạn code nối chuỗi đó, em muốn 2 chuỗi kết hợp bằng kí tự "&" thì làm sao.
Em đã thử bỏ dấu "&" vào code thì nó xuất hiện ở tất cả các ô trống
 
Upvote 0
Cho em đào mộ xíu.
Với đoạn code nối chuỗi đó, em muốn 2 chuỗi kết hợp bằng kí tự "&" thì làm sao.
Em đã thử bỏ dấu "&" vào code thì nó xuất hiện ở tất cả các ô trống

Bạn đang dùng code nào để nối? File của bạn đâu?
 
Upvote 0
em dùng code này nè thầy

Mã:
[COLOR=#007700][FONT=monospace]Public Function [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]STT [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Variant[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Rng [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Col [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
Dim Cll [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each Cll In Rng
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Cll[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]STT Then
        GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]& [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]" " [/FONT][/COLOR][COLOR=#007700][FONT=monospace]& [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Cll[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Offset[/FONT][/COLOR][COLOR=#007700][FONT=monospace](, [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Col[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next Cll
GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Trim[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function [/FONT][/COLOR]


file của em nặng quá. Ko up lên nổi ;(
 
Lần chỉnh sửa cuối:
Upvote 0
em dùng code này nè thầy

Mã:
[COLOR=#007700][FONT=monospace]Public Function [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]STT [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Variant[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Rng [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range[/FONT][/COLOR][COLOR=#007700][FONT=monospace], [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Col [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Long[/FONT][/COLOR][COLOR=#007700][FONT=monospace]) As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]String
Dim Cll [/FONT][/COLOR][COLOR=#007700][FONT=monospace]As [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Range
[/FONT][/COLOR][COLOR=#007700][FONT=monospace]For [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Each Cll In Rng
    [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Cll[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Value [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]STT Then
        GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]& [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]" " [/FONT][/COLOR][COLOR=#007700][FONT=monospace]& [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Cll[/FONT][/COLOR][COLOR=#007700][FONT=monospace].[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Offset[/FONT][/COLOR][COLOR=#007700][FONT=monospace](, [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Col[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
    [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]If
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Next Cll
GPE [/FONT][/COLOR][COLOR=#007700][FONT=monospace]= [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]Trim[/FONT][/COLOR][COLOR=#007700][FONT=monospace]([/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]GPE[/FONT][/COLOR][COLOR=#007700][FONT=monospace])
[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]End [/FONT][/COLOR][COLOR=#007700][FONT=monospace]Function [/FONT][/COLOR]


file của em nặng quá. Ko up lên nổi ;(

Dùng code ở bài 7 nhé!
Áp dụng: =JoinText("&", Vùng dữ liệu của bạn)
 
Upvote 0
Em đang dùng jointext, nhưng muốn biết thêm nếu dùng code này thì sẽ làm sao thôi ạ
 
Upvote 0
thầy ndu xem giùm e file này nhé. Nếu dùng code dưới thì làm sao để ô G4 các đoạn text được ngăn cách bằng dấu "&"
Mã:
Option ExplicitPublic Function GPE(STT As Variant, Rng As Range, Col As Long) As String
Dim Cll As Range
For Each Cll In Rng
    If Cll.Value = STT Then
        GPE = GPE & " " & Cll.Offset(, Col)
    End If
Next Cll
GPE = Trim(GPE)
End Function

P/s: thấy hướng dẫn dùm e. Code ở bài 7 làm sao để nối có điều kiện. áp dụng cho file này luôn nha thầy
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
thầy ndu xem giùm e file này nhé. Nếu dùng code dưới thì làm sao để ô G4 các đoạn text được ngăn cách bằng dấu "&"
Mã:
Option ExplicitPublic Function GPE(STT As Variant, Rng As Range, Col As Long) As String
Dim Cll As Range
For Each Cll In Rng
    If Cll.Value = STT Then
        GPE = GPE & " " & Cll.Offset(, Col)
    End If
Next Cll
GPE = Trim(GPE)
End Function

P/s: thấy hướng dẫn dùm e. Code ở bài 7 làm sao để nối có điều kiện. áp dụng cho file này luôn nha thầy

Bạn xem thế này được chưa, mình đã sửa theo ý bạn rồi đó
 

File đính kèm

Upvote 0
P/s: thấy hướng dẫn dùm e. Code ở bài 7 làm sao để nối có điều kiện. áp dụng cho file này luôn nha thầy

Như file này, nếu dùng code bài 7 thì ta sẽ gõ công thức trên bảng tính như sau:
Mã:
=JoinText(" & ",IF($B$4:$B$12=F4,$C$4:$C$12,1/0))
Công thức gõ tại G4. Gõ xong bấm tổ hợp phím Ctrl + Shift + Enter để kết thúc
 

File đính kèm

Upvote 0
Nối dữ liệu các ô lại với nhau theo 2 điều kiện

Chào các thầy !
Em có 1 fiel muốn lấy các chuỗi cột E (sheet invoice) sang Cột J (sheet chi tieu) nhưng phải thỏa mãn điều kiện ở Cột B và Cột I (sheet Chi tieu) thì phải làm thế nào, em có xem công thức Jointext để nối nhưng chỉ làm được 1 điều kiện
Mong các thầy giúp em với
 

File đính kèm

Upvote 0
Chào các thầy !
Em có 1 fiel muốn lấy các chuỗi cột E (sheet invoice) sang Cột J (sheet chi tieu) nhưng phải thỏa mãn điều kiện ở Cột B và Cột I (sheet Chi tieu) thì phải làm thế nào, em có xem công thức Jointext để nối nhưng chỉ làm được 1 điều kiện
Mong các thầy giúp em với

10 điều kiện còn được chứ nói gì 2 điều kiện
Mã:
=JoinText(" , ",IF([COLOR=#ff0000](I7=invoice!$H$6:$H$297)/(B7=invoice!$B$6:$B$297)[/COLOR],invoice!$E$6:$E$297,1/0))
Công thức gõ tại cell J7 nhé
Ví dụ thêm nối chuỗi 3 điều kiện: 2 điều kiện bạn nêu trên + điều kiện loại bỏ dữ liệu rổng:
Mã:
=JoinText(" , ",IF([COLOR=#ff0000](I7=invoice!$H$6:$H$297)/(B7=invoice!$B$6:$B$297)/(invoice!$B$6:$B$297<>"")[/COLOR],invoice!$E$6:$E$297,1/0))
vân vân... tùy biến thoải mái!
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

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

Back
Top Bottom