다음과 같이 할 수 있습니다. a) 함수에 ID 열을 하드 코딩 할 수 있습니다. b) 매개 변수를 추가하여 ID 열을 함수에 전달합니다. c) 열 머리글 이름을 함수에 전달합니다. 샘플 이미지의 G2에서
Option Explicit
Public Function MultiCat2A(ByRef rRng As Excel.Range, _
Optional ByVal sDelim As String = ",") _
As String
Dim c As Long, cRng As Range
'restrict rRng to the .UsedRange
Set rRng = Intersect(rRng, rRng.Parent.UsedRange)
'set cRng to another column but equal to rRng
Set cRng = Intersect(rRng.EntireRow, rRng.Parent.Columns("A"))
For c = 1 To rRng.Count
If rRng(c).Value < 3 Then
MultiCat2A = MultiCat2A & sDelim & cRng(c).Text
End If
Next c
MultiCat2A = Mid(MultiCat2A, Len(sDelim) + 1)
If CBool(Len(sDelim)) Then
Do While Right(MultiCat2A, Len(sDelim)) = sDelim
MultiCat2A = Left(MultiCat2A, Len(MultiCat2A) - Len(sDelim))
Loop
End If
End Function
Public Function MultiCat2B(ByRef cRng As Range, _
ByRef rRng As Excel.Range, _
Optional ByVal sDelim As String = ",") _
As String
Dim c As Long
'restrict rRng to the .UsedRange
Set rRng = Intersect(rRng, rRng.Parent.UsedRange)
'resize cRng to the same as rRng
Set cRng = cRng(1, 1).Resize(rRng.Rows.Count, rRng.Columns.Count)
For c = 1 To rRng.Count
If rRng(c).Value < 3 Then
MultiCat2B = MultiCat2B & sDelim & cRng(c).Text
End If
Next c
MultiCat2B = Mid(MultiCat2B, Len(sDelim) + 1)
If CBool(Len(sDelim)) Then
Do While Right(MultiCat2B, Len(sDelim)) = sDelim
MultiCat2B = Left(MultiCat2B, Len(MultiCat2B) - Len(sDelim))
Loop
End If
End Function
Public Function MultiCat2C(ByVal sHdr As String, _
ByRef rRng As Excel.Range, _
Optional ByVal sDelim As String = ",") _
As String
Dim c As Long, cRng As Range
'restrict rRng to the .UsedRange
Set rRng = Intersect(rRng, rRng.Parent.UsedRange)
'find the column by header label
c = Application.Match(sHdr, rRng.Parent.Rows(1), 0)
'offset cRng by its column vs rRng's column
Set cRng = rRng(1, 1).Offset(0, c - rRng.Column)
For c = 1 To rRng.Count
If rRng(c).Value < 3 Then
MultiCat2C = MultiCat2C & sDelim & cRng(c).Text
End If
Next c
MultiCat2C = Mid(MultiCat2C, Len(sDelim) + 1)
If CBool(Len(sDelim)) Then
Do While Right(MultiCat2C, Len(sDelim)) = sDelim
MultiCat2C = Left(MultiCat2C, Len(MultiCat2C) - Len(sDelim))
Loop
End If
End Function
: 같은 G5,
=MultiCat2A(B2:B4)
=MultiCat2B($A2:$A4, B2:B4)
=MultiCat2C("ID", B2:B4)
=MultiCat2C($A1, B2:B99)
채우기 바로 필요.
가 BTW, 내가 UDF 함수로 전달 된 모든 매개 변수를 '믿고 ByVal' 관계없이 같은 선언 할 무엇; 나는 이것을 확인하기 위해 약간의 google-fu를해야 할 것이다. – Jeeped