excelvba中的缓冲区溢出

excelvba中的缓冲区溢出,vba,excel,buffer-overflow,Vba,Excel,Buffer Overflow,我已完成VBA模块,该模块将根据提供的医疗服务计算每月记录差异。这很有效。然而,当我试着运行代码3个月,即3月份,并将2月份的数据用作静态数据时,我被提醒我的代码已经开始了缓冲区溢出 我检查了我的代码,但我无法确定为什么会出现这种情况-唯一一致的因素是,当我到第三个月没有进一步测试时,4次中有1次我会收到关闭excel的防病毒警报,指示溢出。有人能帮我确定为什么会这样吗 Sub monthlyCalculation() Dim ws As Worksheet 'Worksheet Variabl

我已完成VBA模块,该模块将根据提供的医疗服务计算每月记录差异。这很有效。然而,当我试着运行代码3个月,即3月份,并将2月份的数据用作静态数据时,我被提醒我的代码已经开始了缓冲区溢出

我检查了我的代码,但我无法确定为什么会出现这种情况-唯一一致的因素是,当我到第三个月没有进一步测试时,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
我修补了一下,我想我已经发现了导致缓冲区溢出问题的原因。根据我对函数的编码方式,当新创建的工作表使用旧的已删除工作表的名称时,会有很多工作表的名称交换。其中一张工作表(尤其是月度比较)的计算依赖于另一张工作表(StaticRecord)的数据。一旦StaticRecord被删除并随后重新命名,我可能会引入一个指针问题,指向已清除的内存,这会混淆excel并导致其关闭。此外,我还更改了选项卡的删除顺序

 For Each ws In Worksheets
  If ws.Name = "MonthlyComparison" Then  
     ws.delete
  End If

Next

For Each ws In Worksheets
  If ws.Name = "StaticRecord" Then
     ws.delete
  End If

Next
最初,我先删除了StaticRecord选项卡,然后删除了每月比较。然而,MonthlyRecord依靠StaticRecord获取数据。所以,一旦我先删除了MonthlyRecord,然后删除了StaticRecord,这个问题似乎至少在目前能够自行解决

这是代码的其余部分,以防您发现我所写内容的任何其他问题:

Sub monthlyCalculation()
Dim ws As Worksheet

Sheets("StaticRecord").Copy After:=Sheets("StaticRecord")
Sheets("StaticRecord (2)").Visible = True
Sheets("StaticRecord (2)").Name = "MonthlyComparison"
Sheets("MonthlyComparison").Select
Range("I6").Value = "=ABS('StaticRecord'!I6-Summary!I6)"
Range("I6").Select
Selection.AutoFill Destination:=Range("I6:I28"), Type:=xlFillDefault

'Key Metrics Calculation
 Range("D6").Value = "= ABS(VALUE(LEFT('StaticRecord'!D6,2))-VALUE(LEFT(Summary!D6,2)))"
 Range("D7").Value = "=ABS(('StaticRecord'!D7)-(Summary!D7))"
 Range("D8").Value = "=ABS(('StaticRecord'!D8)-(Summary!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


'For future calculations, comparisons between static record and the monthlyComparison tab will be made. This means that
'MonthlyComparison will need to be copied across and renamed as a static record with static values.
Application.DisplayAlerts = False
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. It relies on another
'This will need to be corrected by making the values static so values from MonthlyComparison are copied to Static Record (2)
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

'Now we delete the existence of MonthlyComparison as it relies on      StaticRecord for calculations
  For Each ws In Worksheets
  If ws.Name = "MonthlyComparison" Then  ''Or ws.Name = "StaticRecord"'
     ws.delete
  End If

Next

For Each ws In Worksheets
  If ws.Name = "StaticRecord" Then
     ws.delete
  End If

Next

End Sub

如果没有示例笔记本,这是很棘手的。你有机会提交一份吗?当溢出发生时,你检查过了吗?你在这里使用注释有点不正常;它看起来更像是您在未完成的代码中留下的注释,而不是实际指示代码当前的功能。如果你的模块理论上是完整的,你的评论应该指出各个部分的目的。还有-什么是“模板:模板-书尾”?这是一个单独的工作表吗?最后一点-虽然底部的注释表明存在循环,但您没有包括循环的代码部分。我在上面的选项卡中看不到任何特别密集的内容,那么您的代码的其余部分做什么呢?您确定溢出来自此部分吗?当无法为变量分配大于其类型的值时,这将生成溢出:Dim i As Byte:i=258。导入新数据通常表明您的范围格式不需要某些值,例如浮点数:尽管Excel可以显示30个小数点,但其指定数字的精度仅限于15个有效数字,并且由于以下原因,计算的精度可能更低。。。二进制存储。您的数据是否包含具有大量小数位数尾数的浮点?检查所有列的单元格格式格式化单元格…@Grade'Eh'Bacon:这些注释主要是给我的,这样我可以重新查看代码的某些部分,并验证我当时正在尝试做什么。对于该模块,这就是所有的代码。我有另一个模块,我用它导出为PDF文件,并进行复制/粘贴,但这些都不是密集型的。保罗:我不相信我有任何变量会导致你所描述的溢出。今天早上我会再次尝试,如果我不能解决它,我会上传我的工作簿。从我提供的代码中,你看到它溢出的原因了吗?大家好,我发现了我的代码出错的另一个原因。这是因为我的代码中有一个“copy”函数,它复制另一个包含所有公式的电子表格。另外,我使用了一个FOR循环的宏,它遍历了我所有的工作表,导致了大量的迭代。在调试模式下,当我继续运行此程序时,经过大约40次迭代后,工作表将崩溃。在消除对for循环的需求后,不再有任何崩溃: