Excel VBA - 循环未正确添加数据
Posted
技术标签:
【中文标题】Excel VBA - 循环未正确添加数据【英文标题】:Excel VBA - Loop not adding data correctly 【发布时间】:2018-07-10 12:16:20 【问题描述】:我有下面的代码,它允许我打开 12 个 Excel 数据文件,一年中的每个月一个。在每个文件上运行宏以清理数据后,主代码应该从特定列/行中获取数据,将其编译在一起,然后将其输入到新的工作簿中。
我能够循环打开每个文件并在它们上运行自定义宏,但无法将它们添加在一起。
以下是我拥有的完整代码。在“将数据添加到表格”部分下,尝试使用正确的代码,但它似乎只是将每个单元格的最后几个月数据添加到新书中,而不是从每个单元格添加 12 个月的数据。接下来的 4 节是我使用的旧代码,第 2 节和第 4 节除以 12 得到平均值(这些是以分钟/秒为单位的调用时间,其中第 1、第 3 和第 5 节是总数)。
Sub ECHI12MonthCombiner()
'
' ECHI12MonthCombiner Macro
'
'
'Intialize workbooks
Dim MonthSum(1 To 12) As Workbook
Dim File As Variant
ChDir "C:\Users\xxxxxxxxxxxxxxx\Desktop\Monthly Performance Summary"
Dim Month As Integer
For Month = 1 To 12
File = Application.GetOpenFilename
Set MonthSum(Month) = Workbooks.Open(File)
ActiveSheet.name = MonthName(Month, True)
Call ECHIBasicMonthlySummary
Next
'Create new workbook
Dim combinedMonthlySummaries As Workbook
Set combinedMonthlySummaries = Workbooks.Add
Sheets("Sheet1").name = "Call Data"
'Change analysts to variables
Set Anal = Workbooks.Open("C:\Users\stefan.bagnato\Desktop\Analysts\Analysts")
Dim var1, var2, var3, var4, var5, var6, var7 As String
var1 = Workbooks("Analysts").Worksheets("Analysts").Range("A1")
var2 = Workbooks("Analysts").Worksheets("Analysts").Range("A2")
var3 = Workbooks("Analysts").Worksheets("Analysts").Range("A3")
var4 = Workbooks("Analysts").Worksheets("Analysts").Range("A4")
var5 = Workbooks("Analysts").Worksheets("Analysts").Range("A5")
var6 = Workbooks("Analysts").Worksheets("Analysts").Range("A6")
var7 = Workbooks("Analysts").Worksheets("Analysts").Range("A7")
Workbooks("Analysts").Close
'Create table
'Add table headers
Range("A2") = var1
Range("A3") = var2
Range("A4") = var3
Range("A5") = var4
Range("A6") = var5
Range("A7") = var6
Range("A8") = var7
Range("B1") = "Staff Time"
Range("C1") = "Calls Offered"
Range("D1") = "ACD Calls"
Range("E1") = "AHT"
Range("F1") = "Exn Out Calls"
Range("G1") = "Avg Extn Out Time"
Range("H1") = "Avail Time"
Range("I1") = "AUX Time"
Range("J1") = "Lunch Break"
Range("K1") = "Short Break"
Range("L1") = "Special Project"
Range("M1") = "In A Meeting"
Range("N1") = "Default"
Range("O1") = "Tea Break"
'Justify cells
Range("B1:O8").HorizontalAlignment = xlCenter
'Format columns
Range("B2:B8,E2:E8,G2:O8").NumberFormat = "[h]:mm:ss"
Range("A2:A8,B1:O1").Font.Bold = True
'Widen columns
Range("A:A").ColumnWidth = 16.5
Range("B:O").ColumnWidth = 12
'Wrap text
Range("B1:O1").WrapText = True
'Add data to table
Dim callRow As Long, callCol As Long, Sum As Double
For callRow = 2 To 8
For callCol = 2 To 4
For Month = 1 To 12
Sum = MonthSum(Month).Worksheets(MonthName(Month, True)).Cells(callRow, callCol).Value
Next
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = Sum
Next
Next
For callRow = 2 To 8
For callCol = 5 To 5
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = _
(monthSumJan.Worksheets("Agent Group Summary Monthly-jan").Cells(callRow, callCol).Value + _
monthSumFeb.Worksheets("Agent Group Summary Monthly-feb").Cells(callRow, callCol).Value + _
monthSumMar.Worksheets("Agent Group Summary Monthly-mar").Cells(callRow, callCol).Value + _
monthSumApr.Worksheets("Agent Group Summary Monthly-apr").Cells(callRow, callCol).Value + _
monthSumMay.Worksheets("Agent Group Summary Monthly-may").Cells(callRow, callCol).Value + _
monthSumJun.Worksheets("Agent Group Summary Monthly-jun").Cells(callRow, callCol).Value + _
monthSumJul.Worksheets("Agent Group Summary Monthly-jul").Cells(callRow, callCol).Value + _
monthSumAug.Worksheets("Agent Group Summary Monthly-aug").Cells(callRow, callCol).Value + _
monthSumSep.Worksheets("Agent Group Summary Monthly-sep").Cells(callRow, callCol).Value + _
monthSumOct.Worksheets("Agent Group Summary Monthly-oct").Cells(callRow, callCol).Value + _
monthSumNov.Worksheets("Agent Group Summary Monthly-nov").Cells(callRow, callCol).Value + _
monthSumDec.Worksheets("Agent Group Summary Monthly-dec").Cells(callRow, callCol).Value) / 12
Next
Next
For callRow = 2 To 8
For callCol = 6 To 6
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = _
monthSumJan.Worksheets("Agent Group Summary Monthly-jan").Cells(callRow, callCol).Value + _
monthSumFeb.Worksheets("Agent Group Summary Monthly-feb").Cells(callRow, callCol).Value + _
monthSumMar.Worksheets("Agent Group Summary Monthly-mar").Cells(callRow, callCol).Value + _
monthSumApr.Worksheets("Agent Group Summary Monthly-apr").Cells(callRow, callCol).Value + _
monthSumMay.Worksheets("Agent Group Summary Monthly-may").Cells(callRow, callCol).Value + _
monthSumJun.Worksheets("Agent Group Summary Monthly-jun").Cells(callRow, callCol).Value + _
monthSumJul.Worksheets("Agent Group Summary Monthly-jul").Cells(callRow, callCol).Value + _
monthSumAug.Worksheets("Agent Group Summary Monthly-aug").Cells(callRow, callCol).Value + _
monthSumSep.Worksheets("Agent Group Summary Monthly-sep").Cells(callRow, callCol).Value + _
monthSumOct.Worksheets("Agent Group Summary Monthly-oct").Cells(callRow, callCol).Value + _
monthSumNov.Worksheets("Agent Group Summary Monthly-nov").Cells(callRow, callCol).Value + _
monthSumDec.Worksheets("Agent Group Summary Monthly-dec").Cells(callRow, callCol).Value
Next
Next
For callRow = 2 To 8
For callCol = 7 To 7
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = _
(monthSumJan.Worksheets("Agent Group Summary Monthly-jan").Cells(callRow, callCol).Value + _
monthSumFeb.Worksheets("Agent Group Summary Monthly-feb").Cells(callRow, callCol).Value + _
monthSumMar.Worksheets("Agent Group Summary Monthly-mar").Cells(callRow, callCol).Value + _
monthSumApr.Worksheets("Agent Group Summary Monthly-apr").Cells(callRow, callCol).Value + _
monthSumMay.Worksheets("Agent Group Summary Monthly-may").Cells(callRow, callCol).Value + _
monthSumJun.Worksheets("Agent Group Summary Monthly-jun").Cells(callRow, callCol).Value + _
monthSumJul.Worksheets("Agent Group Summary Monthly-jul").Cells(callRow, callCol).Value + _
monthSumAug.Worksheets("Agent Group Summary Monthly-aug").Cells(callRow, callCol).Value + _
monthSumSep.Worksheets("Agent Group Summary Monthly-sep").Cells(callRow, callCol).Value + _
monthSumOct.Worksheets("Agent Group Summary Monthly-oct").Cells(callRow, callCol).Value + _
monthSumNov.Worksheets("Agent Group Summary Monthly-nov").Cells(callRow, callCol).Value + _
monthSumDec.Worksheets("Agent Group Summary Monthly-dec").Cells(callRow, callCol).Value) / 12
Next
Next
For callRow = 2 To 8
For callCol = 8 To 15
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = _
monthSumJan.Worksheets("Agent Group Summary Monthly-jan").Cells(callRow, callCol).Value + _
monthSumFeb.Worksheets("Agent Group Summary Monthly-feb").Cells(callRow, callCol).Value + _
monthSumMar.Worksheets("Agent Group Summary Monthly-mar").Cells(callRow, callCol).Value + _
monthSumApr.Worksheets("Agent Group Summary Monthly-apr").Cells(callRow, callCol).Value + _
monthSumMay.Worksheets("Agent Group Summary Monthly-may").Cells(callRow, callCol).Value + _
monthSumJun.Worksheets("Agent Group Summary Monthly-jun").Cells(callRow, callCol).Value + _
monthSumJul.Worksheets("Agent Group Summary Monthly-jul").Cells(callRow, callCol).Value + _
monthSumAug.Worksheets("Agent Group Summary Monthly-aug").Cells(callRow, callCol).Value + _
monthSumSep.Worksheets("Agent Group Summary Monthly-sep").Cells(callRow, callCol).Value + _
monthSumOct.Worksheets("Agent Group Summary Monthly-oct").Cells(callRow, callCol).Value + _
monthSumNov.Worksheets("Agent Group Summary Monthly-nov").Cells(callRow, callCol).Value + _
monthSumDec.Worksheets("Agent Group Summary Monthly-dec").Cells(callRow, callCol).Value
Next
Next
'Close monthly summary workbooks
Application.DisplayAlerts = False
For Month = 1 To 12
MonthSum(Month).Close
Next
Application.DisplayAlerts = True
编辑 - 响应@Spinjector
将 Sum +
添加到 `Sum = MonthSum(Month).Worksheets(MonthName(Month, True)).Cells(callRow, callCol).Value' 时,我得到以下值,这些值是随机且不正确的。
【问题讨论】:
请仅发布您的代码的相关部分 - 我的意思是实际重现问题的内容,正如您应该按照 Minimal, Complete and Verifiable Example跨度> 明白,尽管这会降低不可避免的“发布您的整个代码以便我们知道您要做什么”的可能性。 我认为您误解了该规则。正如上面 MCVE(缩写)中所解释的那样:您的目标是发布能够复制问题的最简约的示例您的整个代码适用于的帖子,“发布复制问题”——基本上这样我们就可以获取您的代码并将其放入我们的编译器中,这是有道理的,而不是在这里转储您的 40k 行代码并期望我们对其进行筛选,因为它可以协同工作。 “整个代码”行通常被用户引用,他们发布 5 行代码并希望我们填写空白。因此它就在那里! 【参考方案1】:好像没有加法和除法。
见下文。注意我添加的位:Sum = Sum + ...
和 = Sum / 12
。
Dim callRow As Long
Dim callCol As Long
Dim Sum As Double
For callRow = 2 To 8
For callCol = 2 To 4
Sum = 0 'Reset the sum for each pass of the loop.
For Month = 1 To 12
Sum = Sum + MonthSum(Month).Worksheets(MonthName(Month, True)).Cells(callRow, callCol).Value
Next
combinedMonthlySummaries.Worksheets("Call Data").Cells(callRow, callCol).Value = Sum / 12
Next
Next
【讨论】:
嗯。这似乎无法解决问题。我相信Sum +
是打破它的原因。我正在向 OP 添加一个编辑,其中包含您建议的两个编辑所发生的屏幕截图。 Col B 的格式应该是 00:00:00,每个单元格的时间大约在 1500 小时左右。 Col C 和 D 是整数,也应该在 1500-2000 左右。
保留= Sum +
但删除/ 12
怎么样。起初我没有理解您所描述的内容,并认为第一部分是替换其他部分。如果第一部分是计算总和,它必须以某种方式累积值,但如果没有Sum +
,该值将在循环的每次传递中被替换,从而给出您所描述的仅显示最后一个月数据的内容.
很抱歉给您带来了困惑。我们现在可以忽略/ 12
。稍后我可以弄清楚那部分(第二部分)。是的,用 ` = Sum + ` 进行了尝试,但这就是给出那些非常奇怪的数字的原因,这些数字似乎在每个单元格中增加了一些未知因素。我可以手动添加小时数(B 列)或电话号码(C 列和 D 列)来查看真正的答案应该是什么,但我知道范围应该是我上面列出的近似值。
好的,For Month =
循环的目的是什么?那里只有一行代码Sum = MonthSum(Month)...
。在循环的每一轮中,Sum
都会被新值覆盖。没有其他动作。然后下一行,combinedMonthlySummaries.Worksheets(...
获得值= Sum
,这只是来自循环的最后一次传递(Dec?)。似乎那里应该有更多的事情发生。至于您看到的古怪值,可能是格式问题吗?日期/时间看起来像巨大的十进制数字。你检查过单元格格式是Date
吗?
嗯。我无法回答第一个问题,哈哈。也许这就是我的问题所在。第一部分的目标是将每个月文件中单元格 B2 中的值相加,并将它们插入到新文件的 B2 中。它应该为 B2:D8 执行此操作。我明白你在说什么。只是想弄清楚如何更正代码。关于格式,通过代码中的Create table
部分,一切都是正确的。我知道的那部分不是问题。谢谢你帮我解决这个问题,顺便说一句。以上是关于Excel VBA - 循环未正确添加数据的主要内容,如果未能解决你的问题,请参考以下文章