简体   繁体   English

根据另一个单元格中的值提取特定记录

[英]Pulling specific records based on value in another cell

I am reaching out again since I got great help last time.自从上次我得到了很大的帮助后,我再次伸出援手。 I have a worksheet that I import daily.我有一个每天导入的工作表。 I would like it to loop through the names in col B and paste the records for that employee in another sheet and to name that sheet the employee's name (ie John Doe).我希望它遍历 col B 中的名称并将该员工的记录粘贴到另一张表中,并将该表命名为员工的姓名(即 John Doe)。 I would like to have each employee listed on their own named sheet with only those records associated with their name.我想让每个员工在他们自己的命名表上列出,只有那些与他们的名字相关的记录。 I will generate a report for each employee from their sheet.我将从他们的工作表中为每个员工生成一份报告。

Here is the code I got so far:这是我到目前为止得到的代码:

 Sub GetEmployeeLines()
    Const STARTING_ROW As Long = 2
    Dim c As Long: c = STARTING_ROW
    Dim lr As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual


    lr = ActiveSheet.Range("B2:B1000").End(xlUp).Row
    
    Dim cell As Range
    For Each cell In Sheets("RawData").Range("B2:B" & lr).Cells
        If cell <> "" Then
            Sheets("DiscrepencyForm").Range("C:F").Rows(c).Value = cell.EntireRow.Columns(3).Resize(, 4).Value
            c = c + 1
        End If
    Next
   
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    
    End Sub

I have also attached a screenshot of my dataset.我还附上了我的数据集的屏幕截图。

样本数据表

If I understand you correctly, I think using a pivot table can give a similar result with the one you expected.如果我理解正确,我认为使用数据透视表可以得到与您预期的结果相似的结果。 The difference is, (A) with the one that you want of the VBA result ---> you select the sheet name to make your report.... (B) with a pivot table (no VBA) ---> you select the name in the Employee field of the pivot table.不同之处在于,(A) 使用您想要的 VBA 结果 ---> 您选择工作表名称来制作报告.... (B) 使用数据透视表(无 VBA)---> 您在数据透视表的 Employee 字段中选择名称。

Anyway, the test sub below maybe can get you start:无论如何,下面的测试子可能可以让你开始:

Sub test()
Dim rgSource As Range: Dim rgHdr As Range:
Dim rg As Range: Dim cell As Range
Dim arr: Dim el

'set the range of the name in sheet RawData into rgSource variable
'set the column header needed to be on each created sheet as rgHdr variable
With Sheets("RawData")
Set rgSource = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
Set rgHdr = .Range("C1:F1")
End With

'get each unique name in rgSource as arr variable
Set arr = CreateObject("scripting.dictionary")
For Each cell In rgSource: arr.Item(cell.Value) = 1: Next

'loop to each element (the unique name) in arr as variable el
For Each el In arr

    'get the range of cell with that name (the looped el value) as rg variable
    With rgSource
    .Replace el, True, xlWhole, , False, , False, False
    Set rg = .SpecialCells(xlConstants, xlLogical)
    .Replace True, el, xlWhole, , False, , False, False
    End With
    
    'set the range of rg from column C to F
    Set rg = rg.Resize(rg.Rows.Count, rgHdr.Columns.Count).Offset(0, 1)

    'create new sheet, name it with the name (the looped el value)
    'then put the rgHdr and the rg value into the new sheet
    With Sheets.Add
        .Name = el
        .Range("A1").Resize(1, rgHdr.Columns.Count).Value = rgHdr.Value
        .Range("A2").Resize(rg.Rows.Count, rgHdr.Columns.Count).Value = rg.Value
    End With

'loop to next el (the next name)
Next el
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 excel根据另一个单元格值从另一个工作表中提取数据 - excel pulling data from another sheet based on another cell value 根据另一个单元格值获取特定的单元格值 - Obtain specific cell-value based on another cell-value 我可以根据另一个单元格的 5 个特定值更改一个单元格的值吗? - Can I change the value of a cell based on 5 specific values of another cell? 根据另一个单元格的开头替换特定的单元格值 - Replace a specific cell value based on what another cell starts with 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value 根据单元格值将数据从特定列复制到另一个工作表 - Copy data from specific columns to another sheet based on a cell value Excel - 根据另一个单元格的一部分计算特定值 - Excel - count a specific value based on a part of another cell 根据单元格值将特定行复制到另一个工作簿 - Copy specific row to another workbook based on a cell value 有没有办法根据特定单元格的值将一个工作表上的一个单元格设置为等于另一个工作表上的一个单元格? - Is there a way to set one cell on one worksheet to equal one cell on another worksheet based on the value of a specific cell? 根据另一个单元格值返回一个单元格值 - Return a cell value based on another cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM