说实话,我不完全确定如何描述它是我正在尝试完成的?但是,无论如何,这是无论如何。我有一个Excel工作表,其中包含一列ID和第二列值需要关联到第一列。问题是A列中的ID包含重复项,这是可以的,因为一个ID可以符合多个值。我需要的是有一个第三列撤回唯一的ID,第四列拉回所有值的分号分隔列表的id符合资格。希望附加的图像有意义吗?为了什么值得我尝试每个我能想到的公式,而且我对宏的认识一无所知,这正是我所想要实现的。 试图找到唯一的ID以及其在excel中符合的所有值
回答
试试下面的代码:
Sub sample()
Dim lastRowA As Long, lastRowC As Long
lastRowA = Range("A" & Rows.Count).End(xlUp).Row
lastRowC = Range("C" & Rows.Count).End(xlUp).Row
Dim rng As Range, cell As Range
Set rng = Range("C2:C" & lastRowC)
Dim rngSearch As Range
Set rngSearch = Range("A1:A" & lastRowA)
Dim rngFind As Range
Dim firstCell As String
For Each cell In rng
Set rngFind = rngSearch.Find(What:=cell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not rngFind Is Nothing Then
temp = rngFind.Offset(0, 1)
firstCell = rngFind.Address
Do While Not rngFind Is Nothing
Set rngFind = rngSearch.FindNext(After:=rngFind)
If rngFind.Address <> firstCell Then
temp = temp & ";" & rngFind.Offset(0, 1)
Else
Set rngFind = Nothing
End If
Loop
End If
cell.Offset(0, 1) = temp
Next
End Sub
这里的另一种方法,有几个优点
- 它builkds的唯一的SKU
- 明确旧数据从列列表
C:D
- 它的运行速度比循环范围要快得多
Sub Demo()
Dim rngA As Range, rng as Range
Dim datA As Variant
Dim i As Long
Dim sh As Worksheet
Dim dic As Object
Set sh = ActiveSheet ' can change this to your worksheet of choice
Set dic = CreateObject("Scripting.Dictionary")
With sh
' Get data from columns A:B into a variant array
Set rngA = .Range(.Cells(2, 2), .Cells(.Rows.Count, 1).End(xlUp))
datA = rngA
' Create list of unique sku's and built value strings
For i = 1 To UBound(datA)
If dic.Exists(datA(i, 1)) Then
dic(datA(i, 1)) = dic(datA(i, 1)) & ";" & datA(i, 2)
Else
dic.Add datA(i, 1), datA(i, 2)
End If
Next
' Clear exisating data from columns C:D
Set rng = .Range(.Cells(2, 4), .Cells(.Rows.Count, 3).End(xlUp))
If rng.Row > 1 Then
rng.Clear
End If
' Put results into columns C:D
.Range(.Cells(2, 3), .Cells(dic.Count + 1, 3)) = Application.Transpose(dic.Keys)
.Range(.Cells(2, 4), .Cells(dic.Count + 1, 4)) = Application.Transpose(dic.Items)
End With
End Sub
如何补充一点:
- 启动VBS编辑器(Alt键 + F11从Excel)
- 表演项目资源管理器,如果其不可见(Ctrl + R)
- 添加
Module
(右键单击工作簿,插入模块) - 打开模块(DBL点击)
- 添加
Option Explicit
作为第一行,如果不是已经有 - 复制此代码粘贴到模块
如何运行它,从Excel
- 激活表与您的数据
- 开放宏观d ialog(Alt键 + F8)
- 从列表中选择
Demo
和运行
嗨克里斯,当我按照上面的步骤我得到运行时间错误429:ActiveX组件不能创建对象。如果我调试它的话:Set dic = CreateObject(“Scripting.Dictionary”)突出显示 – Eric 2013-04-28 15:51:52
我不确定如何感谢你们两位的帮助,但是如果有什么我需要帮助你的评级或某事请告诉我。如果任何人在这种情况下,这是为我工作: – Eric 2013-04-29 17:36:21
子MG29Apr31 昏暗的RNG作为范围 昏暗的DN作为范围 昏暗TXT作为字符串 昏暗unQ 设置RNG =(范围(“A1”),范围( “A” &Rows.count).END(xlUp)) 对于每个DN在RNG 如果InStr函数(TXT,DN)= 0然后 TXT = TXT& “” &DN 结束如果 接着DN unQ = Application.Transpose(Split(Mid(Txt,2),“,”)) ReDim保留unQ(1到UBound(unQ,1),1到2) Dim n As Long For Each Dn In Rng For n = 1到UBound(unQ) 如果Dn = Val( unQ(n,2)= 2f(unQ(n,2)=“”,Dn.Offset(,1),unQ(n,2)& ";“&Dn.Offset(,1 )) Next n Next Dn Range(“C1”)。Resize(UBound(unQ),2)= unQ End Sub – Eric 2013-04-29 17:37:24
- 1. 查找的ID,其中的值是唯一的在阵列
- 2. 在所有列中查找唯一值
- 3. 找到所有的ID,其中日期值为null
- 4. 在一列中查找所有重复值并合并其所有值
- 5. 在sequalizer中查找所有唯一ID的所有最新数据?
- 6. 复合ID及其在JPA
- 7. mongo查询选择集合中所有唯一ID的计数
- 8. 在无向图中找到所有唯一路径
- 9. 查找值的所有ID
- 10. 查找Excel列中的所有唯一名称
- 11. 在Tensorflow中找到唯一的值对
- 12. 试图找到一个合适的起点以及制作应用的方向
- 13. Excel中有多个列的唯一值
- 14. 试图找到一个2状态向量(在python中)的所有组合
- 15. R - 在数据帧的子集中查找所有唯一值
- 16. Excel中的唯一值
- 17. Excel中的唯一值
- 18. 查找所有R唯一字符串
- 19. html中唯一的id值
- 20. Excel中查找返回唯一值
- 21. 的jQuery找到唯一的ID
- 22. 做复合键中的所有属性具有唯一值
- 23. Excel中唯一值
- 24. 唯一值在Excel
- 25. 试图找到使用R的唯一值
- 26. 我在哪里可以找到System.Diagnostics的所有CategoryName及其CounterName的列表?
- 27. 如何找出列中唯一值的数量以及数据框中唯一值的数量?
- 28. 哪里可以找到CDH及其所有软件版本?
- 29. Excel - 范围内的所有唯一字
- 30. Excel宏到Word邮件合并正试图合并列中的所有行
请 '关闭' 通过标记正确答案的问题。这是人们在帮助别人时喜欢的东西。这将有助于脂肪酶与同样的问题,以确定正确的解决方案:) – Santosh 2013-05-28 01:17:30