【发布时间】:2017-01-09 11:53:33
【问题描述】:
我有许多列和标题的工作簿 A,我想根据标题名称分离这些数据并填充到工作簿 B 中(工作簿 B 有 4 张不同的预填充列标题)
1) 工作簿 A(多列),过滤其在 col 'AN' 中的所有唯一值(即 col AN 有 20 个唯一值,但每个唯一集约 3000 行)。
2) 有工作簿 B,在 4 个工作表中预先填充了列,并非所有的标题都与工作簿 A 中的相同。这里将填充来自工作簿 A 的 col AN 的唯一值及其各自的记录,一个在另一个之后。
这里的目标是用工作簿 A 中的数据填充这 4 个工作表,按每个唯一列 AN 值排序,并将其记录放入预先填充的工作簿 B。
到目前为止,这段代码只是唯一地过滤了我的主“AN”列并且只获取唯一值,我需要唯一值和记录。
Sub Sort()
Dim wb As Workbook, fileNames As Object, errCheck As Boolean
Dim ws As Worksheet, wks As Worksheet, wksSummary As Worksheet
Dim y As Range, intRow As Long, i As Integer
Dim r As Range, lr As Long, myrg As Range, z As Range
Dim boolWritten As Boolean, lngNextRow As Long
Dim intColNode As Integer, intColScenario As Integer
Dim intColNext As Integer, lngStartRow As Long
Dim lngLastNode As Long, lngLastScen As Long
' Finds column AN , header named 'first name'
intColScenario = 0
On Error Resume Next
intColScenario = WorksheetFunction.Match("First name", .Rows(1), 0)
On Error GoTo 0
If intColScenario > 0 Then
' Only action if there is data in column E
If Application.WorksheetFunction.CountA(.Columns(intColScenario)) > 1 Then
lr = .Cells(.Rows.Count, intColScenario).End(xlUp).Row
' Copy unique values from the formula column to the 'Unique data' sheet, and write sheet & file details
.Range(.Cells(1, intColScenario), .Cells(lr, intColScenario)).AdvancedFilter xlFilterCopy, , r, True
r.Offset(0, -2).Value = ws.Name
r.Offset(0, -3).Value = ws.Parent.Name
' Delete the column header copied to the list
r.Delete Shift:=xlUp
boolWritten = True
End If
End If
'I need to take the rest of the records with this though.
' Reset system settings
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.Visible = True
End With
End Sub
添加示例图片
工作簿示例,我想对“工作列”进行唯一过滤以将所有类似的记录放在一起:
工作簿样本 B, 表 1(注意会有多张表)。 如您所见,工作簿 A 已按“作业”列排序。
【问题讨论】:
-
不确定如何/哪些行将被过滤并复制到工作簿 B 工作表。请附上一些涉及的工作簿/工作表示例以及场景之前/之后
-
@user3598756 ,您好,我更新了一些示例,第二张图片是所需的结果,但跨多个工作表,(标题将预先填充)
-
您探索过数据透视表吗?
-
嗨艾伦。不,我没有,不确定这将如何工作,因为我需要查看工作簿 A 中的哪些列标题与其他 4 张工作表中的其他标题匹配,并查看差距在哪里。不过,我愿意接受进一步的解释。