来自另一个工作表的 Countif 的 VBA 具有动态范围列标题名称。根据列名汇总并统计出现次数

问题描述 投票:0回答:0

我希望您能帮助解决我在从主工作表“总部”到另一个工作表“实验室”和“操作”的 countif 函数中面临的问题,基于主工作表中的列。但列引用每个月都会更改,但名称保持不变。所以我不能在 Countif vba 脚本中使用列名作为参考。我喜欢根据主工作表中的数据在他们的工作表中进行计数。我创建了一个 vba 脚本,但无法在代码的最后部分找到基于名称提取的脚本,其中显示“Set rng = ws.Range("AH2:AH" & lr)”我需要

your text
帮助.请查看此代码并在可能的情况下简化此代码以实现此结果。请注意,主表中的引用不能针对特定列,而只能基于列名称“实验室”或“操作”,因为它每次都在变化月。请查看我为此数据创建的示例电子表格以及我编写的 VB 脚本。我希望我可以上传一个示例电子表格

工作表名称“总部” Column1 Column2 Column3 Column4 >> 实验室操作 0000 0000 0000 0000 BF-638-W3E BF-855-8855-0000 0000 0000 0000 0000 BF-638-W3E BF-638-0830-0000 0000 0000 0000 0000 BF-638-W3E BF-638-0830-0000 0000 0000 0000 0000 F6-816-F6 F6-816-9999-0000 0000 0000 0000 0000 F6-816-F6 F6-816-9999-0000 0000 0000 0000 0000 PV-826-910 PV-826-1111-0000 0000 0000 0000 0000 PV-826-910 PV-826-1111-0000 0000 0000 0000 0000 VM-826-6EI VM-826-3333-0000 0000 0000 0000 0000 1G-827-8TT 1G-827-9989-0000 0000 0000 0000 0000 5 -638-CW1 5 -638-0101-0000 0000 0000 0000 0000 5 -638-CW1 5 -638-0830-0000 0000 0000 0000 0000 6C-826-HXP 6C-826-0830-0000 0000 0000 0000 0000 6C-826-HXP 6C-826-0830-0000 0000 0000 0000 0000 6C-826-HXP 6C-826-0830-0000 0001 0000 0001 0000 6C-469-7MV 6C-444-0100-0001 0001 0000 0001 0000 6C-469-7MV 6C-444-0100-0001 0001 0000 0001 0000 6C-469-7MV 6C-444-0100-0001 Main sheet data

'请看代码。它创建一个新的工作表实验室和操作 子列表_Unique_Values() 将 rSelection 调暗为范围 将 qSelection 调暗为范围 将 ws 调暗为工作表 将 rng1 调暗为范围 将 rng2 调暗为范围 ShtName = "实验室" ShtName1 = "操作" 设置 ws = Sheets("总部")

'This will create a sheet 'Laboratory' and copy the data from main sheet and remove duplicate     
If Evaluate("isref('" & ShtName & "'!A1)") Then
'Application.CutCopyMode = False
GoTo Skip
Else
Worksheets("Head Quarters").Select
On Error Resume Next

Set rSelection = Rows("1:1").Find(ShtName, , xlValues, xlWhole, , , True)
rSelection.Range(Range("A1"), Range("A1").End(xlDown)).Copy
Set ws = Worksheets.add(After:=ActiveSheet)
ws.Name = "Laboratory"
End If

With ws.Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
'.PasteSpecial xlPasteValuesAndNumberFormats
End With

'Remove duplicates
ws.UsedRange.RemoveDuplicates Columns:=1, Header:=xlYes

'Remove blank cells (optional)
On Error Resume Next
ws.UsedRange.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp

On Error GoTo 0
'Autofit column
ws.Columns("A").AutoFit
ws.Range("A1").Select

Skip:
Set ws = Sheets("Head Quarters")
Set ws1 = Sheets("Laboratory")
lr = ws.Cells(Cells.Rows.Count, rSelection.Column).End(xlUp).Row
lRow = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row
lRow1 = ws1.Cells(Cells.Rows.Count, "B").End(xlUp).Row
Set rng = ws.Range("AH2:AH" & lr)    ' I need help here to choose the range by column header name 
'Set rng = ws.Range("AH2:lr" & lr)

With ws1
  .Range("A1").Copy .Range("B1")
  .Range("B1") = "Rows"
  .Range("B2:B" & lRow).Formula = "=COUNTIF('" & ws.Name & "'!" & rng.Address & ",A2)"
  .Range("B2:B" & lRow).Value = ws1.Range("B2:B" & lRow).Value

  ' ws1.Columns.AutoFit
  End With
 
  On Error Resume Next
  Set rng1 = ws1.Range(Range("A1"), Range("B1").End(xlDown))
  rng1.Sort key1:=Range("B1"), order1:=xlDescending, Header:=xlYes

  'Similarly create another sheet for 'Operations' and summarize and do countif based on column name 

  End Sub
dynamic tabs range new-operator countif
© www.soinside.com 2019 - 2024. All rights reserved.