在第一次我试图在VBA中运行我的代码,但它最终出现错误,如"程序太大"。所以我试着将它划分为一些潜艇,当我在我的主潜艇中调用另一个潜艇时,它不起作用。在我分成sum subs之前,我的代码运行得很好。
我试图在Private Sub Commandbutton2_Click()
中调用Private sub Calca1()这是我的代码
{{1}}
答案 0 :(得分:1)
我查看了您的代码并试图找出您之后的内容后,我已经更新了我的答案。
我已添加代码以获取代理名称的唯一列表(请注意 - 这在Excel 2003或更早版本中无法运行 - 如果RemoveDuplicates
失败,请告知我们)
最后一个块使用R1C1样式的引用单元格为报表单添加COUNTIFS
公式。希望你能够弄清楚其他公式是如何起作用的
R1C1表示法(可以在此处链接到StackOverflow答案):
http://www.numeritas.co.uk/2013/09/the-%E2%80%98dark-art%E2%80%99-of-r1c1-notation/
我已经添加了部分答案,因为我不确定你之后的情况。顶部将创建您的新表并给出标题
rLastCell
在原始数据表的K列中找到最后一个单元格。在那之后你开始计算东西 - 但是在某些你然后你将结果乘以0会给你0 - 所以我很困惑。
Private Sub CommandButton2_Click()
Dim wrkSht As Worksheet
Dim wrkShtTmp As Worksheet
Dim rLastCell As Range
Dim rLastKPICell As Range
Dim var60 As Integer
Dim varReport60 As Integer
'Add the new sheet and give headings.
Set wrkSht = ThisWorkbook.Worksheets.Add
With wrkSht
.Name = "KPIAgent"
.Range("A1:J1") = Array("Agent Name", "AVG Score", "AVG Common Sense Score", _
"AVG Human Touch Score", "AVG Helpful Score", "AVG Reporting Score", _
"Satisfaction - STP", "Satisfaction - TP", "Satisfaction - P", _
"Satisfaction - SP")
With .Range("A1:J1")
.EntireColumn.AutoFit
.Font.Bold = True
End With
End With
With ThisWorkbook.Worksheets("Raw Data")
'Find last cell on raw data sheet.
Set rLastCell = .Cells.Find("*", , , , , xlPrevious)
If Not rLastCell Is Nothing Then
'Get unique list of agent names and paste into KPIAgent sheet.
Set wrkShtTmp = ThisWorkbook.Worksheets.Add
.Range(.Cells(3, 11), .Cells(rLastCell.Row, 11)).Copy _
Destination:=wrkShtTmp.Cells(1, 1)
wrkShtTmp.Range(wrkShtTmp.Cells(1, 1), wrkShtTmp.Cells(rLastCell.Row, 1)) _
.RemoveDuplicates Columns:=1, Header:=xlNo
wrkShtTmp.Range(wrkShtTmp.Cells(1, 1), wrkShtTmp.Cells(rLastCell.Row, 1)).Copy _
Destination:=wrkSht.Range("A2")
Application.DisplayAlerts = False
wrkShtTmp.Delete
Application.DisplayAlerts = True
End If
End With
'Add figures to the report.
With wrkSht
'Find last cell on KPI sheet.
Set rLastKPICell = .Columns(1).Find("*", , , , , xlPrevious)
'Add formula to column 3 (C) of reporting sheet.
.Range(.Cells(2, 3), .Cells(rLastKPICell.Row, 3)).FormulaR1C1 = _
"=COUNTIFS('Raw Data'!R2C11:R" & rLastCell.Row & "C11,RC1, " & _
"'Raw Data'!R2C14:R" & rLastCell.Row & "C14,""Recording""," & _
"'Raw Data'!R2C15:R" & rLastCell.Row & "C15,""Yes"")*30"
End With
End Sub
答案 1 :(得分:1)
保持代码不变(只是为了让它工作):
Private Sub Calca1(trow2, var60, varreport60)
Dim CS_Yes As Integer
Dim CS_No As Integer
Dim CS As Integer
Dim HT_Yes As Integer
Dim HT_No As Integer
Dim HT As Integer
Dim H_Yes As Integer
Dim H_No As Integer
Dim H As Integer
Dim RP_Yes As Integer
Dim RP_No As Integer
Dim RP As Integer
Sheets("KPIAgent").Cells(trow2 + 1, 1).Value = "Agnes"
CS_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("R4:R65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 30
CS_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("R4:R65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
Sheets("KPIAgent").Cells(trow2 + 1, 3).Value = (CS_Yes + CS_No) / var60
HT_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("T4:T65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 20
HT_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("T4:T65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
Sheets("KPIAgent").Cells(trow2 + 1, 4).Value = (HT_Yes + HT_No) / var60
H_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("V4:V65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 40
H_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("V4:V65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
Sheets("KPIAgent").Cells(trow2 + 1, 5).Value = (H_Yes + H_No) / var60
RP_Yes = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("X4:X65536"), "Yes", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 10
RP_No = (Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("X4:X65536"), "No", Sheets("RawData").Range("K4:K65536"), "Agnes")) * 0
Sheets("KPIAgent").Cells(trow2 + 1, 6).Value = (RP_Yes + RP_No) / varreport60
trow2 = trow2 + 1
End Sub
Private Sub CommandButton2_Click()
Sheets.Add.Name = "KPIAgent"
Sheets("KPIAgent").Activate
Sheets("KPIAgent").Cells(1, 1).Value = "Agent Name"
Sheets("KPIAgent").Cells(1, 2).Value = "AVG Score"
Sheets("KPIAgent").Cells(1, 3).Value = "AVG Common Sense Score"
Sheets("KPIAgent").Cells(1, 4).Value = "AVG Human Touch Score"
Sheets("KPIAgent").Cells(1, 5).Value = "AVG Helpful Score"
Sheets("KPIAgent").Cells(1, 6).Value = "AVG Reporting Score"
Sheets("KPIAgent").Cells(1, 7).Value = "Satisfaction - STP"
Sheets("KPIAgent").Cells(1, 8).Value = "Satisfaction - TP"
Sheets("KPIAgent").Cells(1, 9).Value = "Satisfaction - P"
Sheets("KPIAgent").Cells(1, 10).Value = "Satisfaction - SP"
Sheets("KPIAgent").Columns("A:J").Select
Selection.EntireColumn.AutoFit
Sheets("KPIAgent").Range("A1:J1").Font.Bold = True
Dim i As Integer
Dim flag60 As Integer
Dim trow As Integer
Dim trow2 As Integer
Dim var60 As Integer
Dim varreport60 As Integer
var60 = Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("K4:K65536"), "Agnes")
varreport60 = Application.WorksheetFunction.CountIfs(Sheets("RawData").Range("K4:K65536"), "Agnes", Sheets("RawData").Range("Q4:Q65536"), "Recording")
trow = Sheets("RawData").UsedRange.Rows.Count
trow2 = Sheets("KPIAgent").UsedRange.Rows.Count
For i = 4 To trow
If Sheets("RawData").Cells(i, 11).Value = "Agnes" Then
flag60 = 1
End If
Next i
If flag60 = 1 Then Calca1 trow2, var60, varreport60
End Sub
修改强>
这就像我写代码一样:
Option Explicit
Private Sub CommandButton2_Click()
Sheets.Add.Name = "KPIAgent"
With Sheets("KPIAgent")
.Activate
With .Range("A1:J1")
.Value = Array("Agent Name", "AVG Score", "AVG Common Sense Score", "AVG Human Touch Score", "AVG Helpful Score", "AVG Reporting Score", _
"Satisfaction - STP", "Satisfaction - TP", "Satisfaction - P", "Satisfaction - SP")
.Font.Bold = True
.EntireColumn.AutoFit
End With
End With
With Sheets("RawData")
If IsNumeric(Application.Match("Agnes", .Range("K4:K" & Sheets("RawData").UsedRange.Rows.Count).Value, 0)) Then
Calca1 2, Application.WorksheetFunction.CountIf(.Range("K4:K65536"), "Agnes"), _
Application.WorksheetFunction.CountIfs(.Range("K4:K65536"), "Agnes", .Range("Q4:Q65536"), "Recording")
End If
End With
End Sub
Private Sub Calca1(ByVal tRow As Long, ByVal var60 As Long, ByVal varreport60 As Long)
With Sheets("RawData")
Sheets("KPIAgent").Cells(tRow, 1).Value = "Agnes"
Sheets("KPIAgent").Cells(tRow, 3).Value = (Application.WorksheetFunction.CountIfs(.Range("R4:R65536"), "Yes", .Range("K4:K65536"), "Agnes") * 30) / var60
Sheets("KPIAgent").Cells(tRow, 4).Value = (Application.WorksheetFunction.CountIfs(.Range("T4:T65536"), "Yes", .Range("K4:K65536"), "Agnes") * 20) / var60
Sheets("KPIAgent").Cells(tRow, 5).Value = (Application.WorksheetFunction.CountIfs(.Range("V4:V65536"), "Yes", .Range("K4:K65536"), "Agnes") * 40) / var60
Sheets("KPIAgent").Cells(tRow, 6).Value = (Application.WorksheetFunction.CountIfs(.Range("X4:X65536"), "Yes", .Range("K4:K65536"), "Agnes") * 10) / varreport60
End With
End Sub
.AutoFit
(自我解释)之后.Bold
Match
答案 2 :(得分:1)
将您在Calca1子系统中使用的所有变量声明为全局变量,如下所示。这样您就不需要传递变量,并且变量发生变化也会转移到Main子。希望它能帮助您,而无需对代码进行太多更改
Public CS_Yes As Integer