VBA 用唯一的拆分条目填充列的函数
VBA Function to fill column with split entries that are unique
我需要帮助来创建一个非常具体的 VBA 函数。
我需要一个既能拆分单元格值又能用唯一值填充另一列的函数。
我目前正在使用 =IFERROR(INDEX(List,MATCH(0,INDEX(COUNTIF($A:A2,List),0,0),0)),"")
以便从一列到另一列获取唯一值。
不幸的是,其中一些值将与“,”连接,但仍需要是唯一的。
不幸的是,我对 VBA 的了解远非广泛。有人有什么建议吗?
假设我们有这样的数据:
在第 A 列中。 运行 这个宏将提取唯一值然后放入列 B:
Sub dural()
Dim c As Collection, K As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
If Err.Number = 0 Then
Cells(K, "B").Value = a
K = K + 1
Else
Err.Number = 0
End If
Next a
Next r
On Error GoTo 0
End Sub
编辑#1:
这里是UDF形式的相同逻辑:
Public Function UniKues(rIn As Range)
Dim c As Collection, K As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In rIn
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
Next a
Next r
ReDim bry(1 To c.Count, 1 To 1)
For i = 1 To c.Count
bry(i, 1) = c.Item(i)
Next i
UniKues = bry
On Error GoTo 0
End Function
只需 hi-light 列 B 的一部分,然后在 Array[=52] 中输入 UDF =]形式
编辑#2
这是 UDF 和 chris neilsen 的 建议:
Public Function UniKues(rIn As Range)
Dim c As Collection, K As Long, MM As Long
Dim CC As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In rIn
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
Next a
Next r
MM = Application.Caller.Rows.Count
CC = c.Count
dimn = Application.WorksheetFunction.Max(MM, CC)
ReDim bry(1 To dimn, 1 To 1)
For i = 1 To CC
bry(i, 1) = c.Item(i)
Next i
If MM > CC Then
For i = CC + 1 To MM
bry(i, 1) = ""
Next i
End If
UniKues = bry
On Error GoTo 0
End Function
我需要帮助来创建一个非常具体的 VBA 函数。 我需要一个既能拆分单元格值又能用唯一值填充另一列的函数。
我目前正在使用 =IFERROR(INDEX(List,MATCH(0,INDEX(COUNTIF($A:A2,List),0,0),0)),"")
以便从一列到另一列获取唯一值。
不幸的是,其中一些值将与“,”连接,但仍需要是唯一的。
不幸的是,我对 VBA 的了解远非广泛。有人有什么建议吗?
假设我们有这样的数据:
在第 A 列中。 运行 这个宏将提取唯一值然后放入列 B:
Sub dural()
Dim c As Collection, K As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
If Err.Number = 0 Then
Cells(K, "B").Value = a
K = K + 1
Else
Err.Number = 0
End If
Next a
Next r
On Error GoTo 0
End Sub
编辑#1:
这里是UDF形式的相同逻辑:
Public Function UniKues(rIn As Range)
Dim c As Collection, K As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In rIn
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
Next a
Next r
ReDim bry(1 To c.Count, 1 To 1)
For i = 1 To c.Count
bry(i, 1) = c.Item(i)
Next i
UniKues = bry
On Error GoTo 0
End Function
只需 hi-light 列 B 的一部分,然后在 Array[=52] 中输入 UDF =]形式
编辑#2
这是 UDF 和 chris neilsen 的 建议:
Public Function UniKues(rIn As Range)
Dim c As Collection, K As Long, MM As Long
Dim CC As Long
Set c = New Collection
K = 1
On Error Resume Next
For Each r In rIn
ary = Split(r.Text, ",")
For Each a In ary
c.Add a, CStr(a)
Next a
Next r
MM = Application.Caller.Rows.Count
CC = c.Count
dimn = Application.WorksheetFunction.Max(MM, CC)
ReDim bry(1 To dimn, 1 To 1)
For i = 1 To CC
bry(i, 1) = c.Item(i)
Next i
If MM > CC Then
For i = CC + 1 To MM
bry(i, 1) = ""
Next i
End If
UniKues = bry
On Error GoTo 0
End Function