2015-08-18 28 views
1

我已經完成了我的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 
+0

這是棘手沒有一個例子筆記本電腦的任何其他問題的代碼的其餘部分。你可以提交一個嗎?當溢出發生時,你檢查過了嗎? – Klaster

+0

你對這些評論的使用在這裏有點不正統;它看起來更像是你在未完成的代碼中留下的筆記,而不是實際指出你的代碼目前的功能。如果你的模塊在理論上是完整的,你的意見應該指出各個部分的用途。另外 - 什麼是'模板:模板 - 書末'?這是一個單一的工作表嗎? –

+0

最後說明 - 您沒有包含循環代碼部分,儘管您在底部的註釋表明存在循環。我在上面的標籤中看不到任何特別密集的內容,那麼其他代碼會做什麼?你確定溢出來自這個部分? –

回答

1

我修修補補周圍,我覺得發現是什麼原因造成我的緩衝區溢出問題。通過編寫該功能的方式,由於新創建的工作表採用了較舊的已刪除工作表的名稱,因此有很多工作表的名稱交換。特別是其中一張工作表(MonthlyComparisons)的計算依賴於來自另一張工作表 - 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 
+1

嗨大家好,我發現了我的代碼爲什麼會出現的另一個原因。這是因爲我的代碼中有一個「複製」功能,它複製另一張電子表格(包括所有的公式)。另外我使用了一個FOR循環,這個循環遍歷了我所有的工作表,導致了很多迭代。在調試模式下,我一直運行這個,經過40次左右的迭代 - 表單會崩潰。消除for-loop的需求後,不再有任何崩潰。 :) – azurekirby