Excel VBA debugger stops without error or warning

感情迁移 提交于 2020-06-23 14:16:06

问题


While trying to test some code, I am having an issue that I don't recall running into before. When I step through the code it halts after the ClearContents line...there is no error, no warning, nothing.

Public Sub CreateCurMth(wsCur As Worksheet)
Dim iData As Integer, iRow As Integer
Dim wbData As Workbook
On Error GoTo err_here

  iRow = wsCur.Cells(Rows.Count, 1).End(xlUp).Row
  wsCur.Range("A10:X" & iRow).ClearContents  '<----- Fails after this line

'Assume that file is already open
  Set wbData = Workbooks("qReport.xlsx")
  ... etc
err_here:
  Beep
End Sub

wsCur is a worksheet that is assigned in the calling sub. The code accurately returns the iRow as the last used row on wsCur. Also, the contents are cleared...so that line executes fine. There is no Beep played after it dies. I have a breakpoint on the Beep also, and it is never hit.

So, as what usually happens while I'm typing out a question...I keep trying things and usually end up finding my answer while trying to fully document my question. Semi-decent results this time...for no reason that I can find, the code above is now working...however, I keep having the same issue in other parts of my VBA that aren't similar to the section above. I've been working on this project for a week or so, and all testing prior to today, this code has worked...or at least errored out like would normally be expected.

Each time the debugger stops, there is no error and no warning. The lines that are failing don't even have anything in common. So I'm left to assume that is it something environmental with my system.

I've searched and Googled, and am left with no answers. I'm hoping that some one here has had this issue before and resolved it, or can at least point me in the correct direction.

My code compiles without error. This is the line that is currently halting:

wsTemp.Delete

Temp worksheet IS deleted...then halts.

EDIT: Ok, so I thought this was just a corrupted Excel file, and doing an Open & Repair resolved it for a few hours. But now I'm having the same issue where the debugger fails after

wsTemp.Delete

I turned on Tools > Options > General > Break on ALL Errors and still I get no error and no warning...code step through just halts.

Just to be thorough...here is the entire procedure up to the failure point:

'Creates combined joblist from last and current month
Sub CreateCompareList(wsCur As Worksheet, wsLast As Worksheet, wsComp As Worksheet)
Dim wsTemp As Worksheet
Dim lLastRow As Long, lCurRow As Long, lLMRow As Long
Dim iCol As Integer, x As Integer

  wsComp.Unprotect SheetPwd
  lLastRow = wsComp.Cells(Rows.Count, 27).End(xlUp).Row         'Get the last row
  If lLastRow = 9 Then
    'No data on sheet - Don't ruin headers
  Else
    wsComp.Rows("10:" & lLastRow).ClearContents                 'Clear that section of Job names and numbers
  End If
  Set wsTemp = Worksheets.Add                                   'Add a Temp Sheet to filter jobs

  lCurRow = wsCur.[A10].End(xlDown).Row                         'Get the last row
  wsCur.Range("A10:B" & lCurRow).Copy                           'Grab all jobs for the current month
  wsTemp.[A1].PasteSpecial                                      'Paste job list from Current Month
  Application.CutCopyMode = False

  lLMRow = wsLast.[A10].End(xlDown).Row                         'Get the last row
  wsLast.Range("A10:B" & lLMRow).Copy                           'Grab all jobs from the Last month
  lLastRow = wsTemp.[A1].End(xlDown).Row                        'Get the last row
  wsTemp.Cells(lLastRow + 1, 1).PasteSpecial                    'Paste jobs from last month below jobs from current month
  Application.CutCopyMode = False

  lLastRow = wsTemp.[A1].End(xlDown).Row                        'Get new last row
  'Filter out duplicates based on the JobNo
  wsTemp.Range("$A$1:$B$" & lLastRow).RemoveDuplicates Columns:=2, Header:=xlNo
  lLastRow = wsTemp.[A1].End(xlDown).Row                        'Get new last row
  wsTemp.Range("A1:B" & lLastRow).Copy                          'Copy unique Jobs

  wsComp.[A10].PasteSpecial                                     'Paste Unique Jobs to Compare sheet

'Clean up Temp sheet
'  Application.DisplayAlerts = False
  wsTemp.Delete

I have tried it with the DisplayAlerts on and off, when the Alerts are on...I get the alert that there may be data on the sheet...I click delete, the sheet is deleted...and then code halts. It occasionally has problems again on other .ClearContents lines like above.


回答1:


Postmortem on this issue seems to lead to the conclusion that this was in fact a corrupted Excel file. Luckily I had a fairly recent back up as well. However, in order to save the time that had been spent coding between that backup and the corrupted version, here are the steps I took to save the file, in case anyone else encounters the strange behavior I did:

  1. Closed all Excel windows and started a fresh instance without opening a file.
  2. Used Ctrl+O to search for the file...but instead of just clicking Open, click the down arrow and select Open & Repair.
  3. Click Repair
  4. Saved as a new copy *-Repaired (just because).

After this, the file ran as expected. The circular reference in the EOMONTH function went away and the debugger ran as expected. I have run full testing on all code and it is working normally.

I have no explanation as to how it became corrupted, just lucky I guess.

See https://support.office.com/en-us/article/Repairing-a-corrupted-workbook-e5b49891-dde7-4796-b60a-49b0d2478a62 for other options when dealing with corrupted Excel files.

EDIT: This issue came back again and what currently has it working was forcing Windows Updates. The system is set on Automatic, but when I manually went and checked, it found 3 Office updates. After installing those, I ran Windows Update again and found another update for .Net framework...so I did that as well...doubt that had any effect...but...

So...after using Excel's Open & Repair, applying updates and restarting a couple of times, turning on Break on all errors...my code is finally working again all the way through.



来源:https://stackoverflow.com/questions/39964472/excel-vba-debugger-stops-without-error-or-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!