需要一个VBA错误处理按钮来停止代码

时间:2016-10-17 15:30:58

标签: vba excel-vba error-handling excel

我正在创建一个模板,其中包含的宏用于那些对VBA一无所知的人,一旦我离开,就没有人可以为他们调试问题,所以我正在尝试构建在一些错误处理中,如果他们遇到问题就帮助他们。

在这段代码中,我正在创建一个名为ConfirmedPivot的工作表。起初,我包含了一个简单的错误处理GoTo 0,它说“这个名字已经存在”,但我担心这个指令对用户来说不够清楚如何修复错误。

所以我创建了ConfPivError消息,它提供了更好的指示,但我无法弄清楚这个消息的最佳按钮是什么。我想要一个按钮,它将停止代码,以便他们可以删除重复的名称并重新开始(相当于结束按钮)。我尝试了几个按钮,但似乎没有一个能够停止代码。所有OK,Abort和Cancel按钮都不会停止代码(因此代码会再次尝试运行并再次出现错误)。

您对我可以使用哪个按钮有什么想法吗?或者,如果用户按下OK后出现此错误,我可以添加一些内容来结束代码吗?

' Create ConfirmedPivot

Dim wsTest As Worksheet
Const strSheetName As String = "ConfirmedPivot"

Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo ConfPivError  ' custom error message

If wsTest Is Nothing Then
    Worksheets.Add.Name = "ConfirmedPivot"
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Recovered_Sheet1!R1C1:R65536C114", Version:=xlPivotTableVersion10). _
        CreatePivotTable TableDestination:="'ConfirmedPivot'!R1C1", TableName:="PivotTable4" _
    , DefaultVersion:=xlPivotTableVersion10
End If

' end Create ConfirmedPivot

' Error handling experiment

ConfPivError:
    Answer = MsgBox("ConfirmedPivot Sheet already exists in this workbook. If you don't need it, delete it then try the button again" _
    & "(New ConfirmedPivot will be created). If you still need it, rename it and try the button again.", vbOKOnly, "ConfirmedPivot already exists!")

' end experiment

2 个答案:

答案 0 :(得分:2)

使用File "/home/nik/project/lib/python3.5/site- packages/scrapy/spiders/__init__.py", line 76, in parse raise NotImplementedError NotImplementedError 2016-10-17 18:48:04 [scrapy] DEBUG: Redirecting (302) to <GET http://edition.cnn.com/2016/10/15/opinions/the-black-panthers-heirs-after-50- years-joseph/index.html> from <GET http://www.cnn.com/2016/10/15/opinions/the- black-panthers-heirs-after-50-years-joseph/index.html> 2016-10-17 18:48:04 [scrapy] DEBUG: Redirecting (302) to <GET http://edition.cnn.com/2016/10/15/africa/montreal-climate-change-hfc- kigali/index.html> from <GET http://www.cnn.com/2016/10/15/africa/montreal- climate-change-hfc-kigali/index.html> 2016-10-17 18:48:04 [scrapy] DEBUG: Redirecting (302) to <GET http://edition.cnn.com/2016/10/14/middleeast/battle-for-mosul-hawija-iraq/index.html> from <GET http://www.cnn.com/2016/10/14/middleeast/battle-for-mosul-hawija-iraq/index.html> 2016-10-17 18:48:04 [scrapy] ERROR: Spider error processing <GET http://edition.cnn.com/2016/10/15/politics/donald-trump-hillary-clinton-drug- test/index.html> (referer: http://edition.cnn.com/sitemaps/sitemap-news.xml) Traceback (most recent call last): File "/home/nik/project/lib/python3.5/site- packages/twisted/internet/defer.py", line 587, in _runCallbacks current.result = callback(current.result, *args, **kw) File "/home/nik/project/lib/python3.5/site- packages/scrapy/spiders/__init__.py", line 76, in parse raise NotImplementedError 并以这种方式处理错误有点奇怪。

也许这样做:

On Error Resume Next

答案 1 :(得分:0)

另一个解决问题的方法是使用@Tim Williams在另一个问题中发布的函数:

Test or check if sheet exists

Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
    Dim sht As Worksheet

     If wb Is Nothing Then Set wb = ActiveWorkbook
     On Error Resume Next
     Set sht = wb.Sheets(shtName)
     On Error GoTo 0
     SheetExists = Not sht Is Nothing
End Function

Sub OKButton()
    ' Create ConfirmedPivot
    On Error GoTo ConfPivError  ' custom error message

    Dim wsTest As Worksheet
    Const strSheetName As String = "ConfirmedPivot"

    If SheetExists(strSheetName) Then
        MsgBox "ConfirmedPivot Sheet already exists in this workbook. If you don't need it, delete it then try the button again" _
        & "(New ConfirmedPivot will be created). If you still need it, rename it and try the button again.", vbOKOnly, "ConfirmedPivot already exists!"
    Else
        Worksheets.Add.Name = "ConfirmedPivot"
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Recovered_Sheet1!R1C1:R65536C114", Version:=xlPivotTableVersion10). _
            CreatePivotTable TableDestination:="'ConfirmedPivot'!R1C1", TableName:="PivotTable4" _
        , DefaultVersion:=xlPivotTableVersion10

        ' end Create ConfirmedPivot
    End If

    ' Error handling experiment
    ConfPivError:
        If Err.Number <> 0 Then
            MsgBox Err.Description, vbOKOnly, Err.Number
        End If

    ' end experiment
End Sub

Obs.1:错误处理仅处理意外错误。

Obs.2:我删除了你的Answer变量,因为你没有使用它。