【发布时间】:2020-03-21 01:37:57
【问题描述】:
我确定这是可能的,我只是不确定代码应该是什么。我有 2 张工作表:(1)组件,其中包含分析师标记的所有组件名称,包括调用发生的日期,以及(2)计算器,它计算特定组件在特定组件中出现的次数周数。
我创建了一个代码,该代码从组件表中获取不同的组件名称,然后将它们复制并转置到计算器表中。所有组件名称都在第 1 行,从 D1 列开始,然后到 E1、F1 等。我希望第 2 行显示组件(列在第 1 行中)在一周内出现的计数或次数。
我的代码只适用于列,我不知道如何让它获取整行的非空值。
'//这里是我用来将不同组件从组件表转置到计算器表的代码
Public Sub GetDistinctComponents()
Application.ScreenUpdating = False
Dim lr As Long
lr = Sheets("Components Data").Cells(Rows.Count, "F").End(xlUp).Row
Sheets("Calculator").Unprotect Password:="secret"
Sheets("Components Data").Range("F1:F" & lr).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=ActiveSheet.Range("DW1"), Unique:=True
With ThisWorkbook.Worksheets("Calculator")
.Range(.Range("DW1"), .Range("DW1").End(xlDown)).Copy
.Range("DX1").PasteSpecial xlPasteValues, Transpose:=True
.Columns("DW").EntireColumn.Delete
End With
Sheets("Calculator").Protect Password:="secret", DrawingObjects:=False
End Sub
下面是我的计算器表。如您所见,转置不同组件的代码工作正常。我只是不知道如何从 DX 开始获取第 1 行的值,因此我可以将其存储在一个变量中,我将使用该变量来计算该组件在一周内出现的次数。我想它应该是这样的 组件 = wsCalculator.Cells(i, "D").Value 但是这个代码只有在我想获取 D 列中所有单元格的值时才有效,而不是 D1 旁边的单元格的值
这是我目前拥有的代码
Public Sub CountComponent()
Application.ScreenUpdating = False
Sheets("Calculator").Unprotect Password:="secret"
Set wsComponentData = Sheets("Components Data")
Set wsCalculator = Sheets("Calculator")
Dim ComponentCount As Integer
'//Get the index of the last filled row based on column A
LastComponentRowIndex = wsComponentData.Cells(Rows.Count, "A").End(xlUp).Row
'//Get Range for ComponentData
Set ComponentRange = wsComponentData.Range("F2:F" & LastComponentRowIndex)
'//Get the index of the last filled row based on column C
LasttotalauditRowIndex = wsCalculator.Cells(Rows.Count, "C").End(xlUp).Row
'//Get range for Calculator
Set MyRange = wsCalculator.Range("C2:C" & LasttotalauditRowIndex)
TotalCalls = WorksheetFunction.Sum(MyRange)
'//Looping through all filled rows in the Components Data sheet
For i = 2 To wsCalculator.Cells(Rows.Count, "A").End(xlUp).Row
'//Get Component from cell in column "DW"
'Component = wsCalculator.Cells(i, "DW").Value
'//Count the # of calls that got hit in the corresponding Component
If wsCalculator.Cells(i, "DW").Value <> "" Then
ComponentCount = Application.WorksheetFunction.CountIf( _
ComponentRange, component)
wsCalculator.Cells(i, "DX").Value = ComponentCount
End If
Next
End Sub
【问题讨论】: