我已经完成了我的VBA模块,该模块可根据所提供的医疗服务计算月度记录差异。这很好。然而,当我尝试运行第3个月(即3月)的代码并将2月数据用作静态数据时 - 我收到了一个事实,即我的代码已经启动缓冲区溢出。Excel VBA中的缓冲区溢出
我去了我的代码,但我无法确定为什么是这种情况 - 唯一一贯的因素是,当我去第三个月(没有进一步测试),4次中的1次我会得到一个防病毒警报关闭Excel表示溢出。任何人都可以帮助我确定为什么会出现这种情况?
Sub monthlyCalculation()
Dim ws As Worksheet 'Worksheet Variable required for IF statement
Sheets("StaticRecord").Copy After:=Sheets("StaticRecord")
Sheets("StaticRecord (2)").Visible = True
'Rename Summary (3) to Monthly Comparison
Sheets("StaticRecord (2)").Name = "MonthlyComparison"
'Remember to do the subtraction calculations here
Sheets("MonthlyComparison").Select
'Don't use ActiveCell but rather a direct reference to subtract
Range("I6").Value = "=ABS(Summary!I6-'StaticRecord'!I6)"
Range("I6").Select
Selection.AutoFill Destination:=Range("I6:I28"), Type:=xlFillDefault
'Key Metrics Calculation for the created MonthlyComparison Tab
Range("D6").Value = "= ABS(VALUE(LEFT(Summary!D6,2))-VALUE(LEFT('StaticRecord'!D6,2)))"
Range("D7").Value = "=ABS((Summary!D7)-('StaticRecord'!D7))"
Range("D8").Value = "=ABS((Summary!D8)-('StaticRecord'!D8))"
Range("D9").Value = "= SUM('Template:Template - Book End'!H55)-2"
Range("D10").Value = "= $D7/$D8"
Range("D11").Value = "= 1 - D$10"
Range("D12").Value = "= Summary!D12"
Range("D13").Value = "= Summary!D13"
Range("D14").Value = "= Summary!D14"
Range("D15").Value = "= Summary!D15"
'# Sessions Calculations
Range("J6").Value = "=ABS('StaticRecord'!J6-Summary!J6)"
Range("J6").Select
Selection.AutoFill Destination:=Range("J6:J27"), Type:=xlFillDefault
Range("J6:J27").Select
'Now that we have done the calculation we need to get rid of the initial Summary by replacing it with a blank template copy
'However we know that the summary tab CANNOT be cleared unless the user tabs are cleared so we must clear these tabs instead
'We will do this by looping through all user tabs and clearing the set fields'
For Each ws In Worksheets
If Len(ws.Name) <= 5 Then
ws.Range("B7:C100").ClearContents
End If
Next
'Lastly we need to ensure that if a new comparison is to be completed, it will compare this against the static record which is last
'months statistics. This means that MonthlyComparison will need to be copied across and renamed as a static record with static values.
Application.DisplayAlerts = False
'StaticRecord has now been deleted so we need to create a new StaticRecord
Sheets("MonthlyComparison").Copy After:=Sheets("MonthlyComparison")
Sheets("MonthlyComparison (2)").Visible = True
Sheets("MonthlyComparison (2)").Name = "StaticRecord (2)"
'Once the monthlyComparison is deleted, the copy of staticRecord (2) will show all REF values
'This will need to be corrected by making the values static
Sheets("MonthlyComparison").Select
Range("I6:J28").Select
Selection.Copy
Sheets("StaticRecord (2)").Select
Range("I6:J28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("MonthlyComparison").Select
Range("D6:D15").Select
Selection.Copy
Sheets("StaticRecord (2)").Select
Range("D6:D15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
For Each ws In Worksheets
If ws.Name = "StaticRecord" Then
ws.delete
End If
Next
'Rename the newly created StaticRecord (2) into StaticRecord
Sheets("StaticRecord (2)").Name = "StaticRecord"
'Now that we have copied the data from MonthlyComparison we can eliminate this tab as it is no longer required
For Each ws In Worksheets
If ws.Name = "MonthlyComparison" Then
ws.delete
End If
Next
End Sub
这是棘手没有一个例子笔记本电脑的任何其他问题的代码的其余部分。你可以提交一个吗?当溢出发生时,你检查过了吗? – Klaster
你对这些评论的使用在这里有点不正统;它看起来更像是你在未完成的代码中留下的笔记,而不是实际指出你的代码目前的功能。如果你的模块在理论上是完整的,你的意见应该指出各个部分的用途。另外 - 什么是'模板:模板 - 书末'?这是一个单一的工作表吗? –
最后说明 - 您没有包含循环代码部分,尽管您在底部的注释表明存在循环。我在上面的标签中看不到任何特别密集的内容,那么其他代码会做什么?你确定溢出来自这个部分? –