excel - Excel:根据另一列的重复项复制列中的单元格
问题描述
我想根据 B 列中的重复项从 A 列复制数据,然后在 C 列中将它们以逗号分隔或水平列出。请看下面的例子:
Column A Column B Column C (Output)
1 A 1,2
2 A
3 B 3,4,5
4 B
5 B
6 C 6
7 D 7
解决方案
Sub CopyCells()
Application.ScreenUpdating = False
Dim i As Long, v As Variant, fVisRow As Long, rng As Range, Val As String
v = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value
With CreateObject("Scripting.Dictionary")
For i = 1 To UBound(v, 1)
If Not .Exists(v(i, 1)) Then
.Add v(i, 1), Nothing
With Range("A1").CurrentRegion
.AutoFilter 2, v(i, 1)
fVisRow = .Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row
For Each rng In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If Val = "" Then Val = rng Else Val = Val & ", " & rng
Next rng
Range("C" & fVisRow) = Val
Val = ""
End With
End If
Next i
End With
Range("A1").AutoFilter
Application.ScreenUpdating = True
End Sub
推荐阅读
- flutter - 在颤动中单击按钮时添加小部件时处理动态复选框列表
- laravel - 如何在 docker 镜像中设置 laravel 文件权限
- c# - 如何强制统一以避免两次拾取随机对象
- c++ - 将枚举 TagLib::ID3v2::AttachedPictureFrame::Type 转换为字符串?
- r - 如何更改 R 华夫饼图中“瓷砖灌浆”的颜色以匹配背景
- ruby-on-rails - 在 ruby gem 中保存和重用数据的有效方法
- react-native - React Native,无法保存相机快照
- android - Android Emulator 是否能够作为 On-The-Go 打印机连接到 PC 上的打印机?
- sql - 如何在 Big Query SQL 中搜索声明值之类的内容?
- swift - 在某些上下文中包装一个高阶函数并应用它