Dépassement de memory tampon dans Excel VBA

J'ai complété mon module VBA qui calculerait les différences de record mensuelles en fonction des services médicaux fournis. Cela fonctionnait bien. Cependant, lorsque j'ai essayé d'exécuter le code pour un 3ème mois (c'est-à-dire mars) et en utilisant datatables de février en tant que données statiques, j'ai été averti du fait que mon code a démarré un débordement de memory tampon.

Je suis allé sur mon code, mais je ne pouvais pas identifier pourquoi c'est le cas. Le seul facteur constant est que lorsque je pars au 3ème mois (je n'ai pas testé plus loin), 1 fois sur 4, j'aurai une alerte antivirus terminée excel indiquant un débordement. Quelqu'un pourrait-il m'aider à identifier pourquoi c'est le cas?

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 Mesortingcs 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 copyd 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 copyd 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 

Je me suis dérangé et je pense avoir découvert ce qui cause mon problème de dépassement de tampon. Avec la façon dont j'ai codé la fonction, il y a beaucoup d'échanges de noms de feuilles car les feuilles nouvellement créées prennent le nom des anciennes feuilles supprimées. L'une des feuilles de travail en particulier (MonthlyComparaisons) comporte des calculs qui reposent sur datatables d'une autre feuille de travail – StaticRecord. Une fois que StaticRecord est supprimé et re-nommé par la suite, je pourrais avoir introduit un problème de pointeur où je signale une memory qui a été effacée, ce qui confond l'excellence et l'entraîne à l'arrêt. De plus, j'ai modifié l'ordre de suppression des tabs.

  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 

Au début, j'ai eu l'onglet StaticRecord supprimé en premier puis la comparaison mensuelle. MonthlyRecord repose sur StaticRecord pour datatables cependant. Donc, une fois que j'ai supprimé MonthlyRecord d'abord et THEN StaticRecord, le problème semblait (du less pour l'instant) se résoudre.

Ceci est le rest du code dans le cas où l'un d'entre vous peut repérer d'autres problèmes avec ce que j'ai écrit 🙂

 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 Mesortingcs 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 copyd 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 copyd 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