EXCEL VBA:输入框中的错误处理问题 [英] EXCEL VBA: Problem with error handling in inputbox

查看:299
本文介绍了EXCEL VBA:输入框中的错误处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,MVP和专家!

我需要有关Excel的输入框的错误处理方面的帮助.我只想提示用户他/她没有输入正确的范围/单元格位置.另外,如果输入框为空并且按OK,则应提示用户输入单元格位置或单元格.非常感谢...

这是我的代码:

Hello MVP''s and Experts!

I need help in error handling for Excels'' inputbox. I just want to prompt the user that he/she is not inputting proper range/cell location. Also, if the inputbox is blank and they press OK, should prompt the user to enter cell location or a cell. Many Thanks...

This is my code:

Sub Find_Duplicates_Inte()
Dim i As Integer
Dim c As Object

i = 35

On Error Resume Next

Set c = Application.InputBox(prompt:="Select starting cell", _
              Title:="Chrispogs", Type:=8)
On Error GoTo 0

     If c Is Nothing Then Exit Sub

c.Select

Application.ScreenUpdating = False

While ActiveCell <> ""
    If ActiveCell = ActiveCell.Offset(-1, 0) Then
        ActiveCell.Interior.ColorIndex = i
        ActiveCell.Offset(-1, 0).Interior.ColorIndex = i
        If ActiveCell = ActiveCell.Offset(1, 0) Then
        Else
            If i = 36 Then
                i = 35
            Else
                i = i + 1
            End If
        End If
        ActiveCell.Offset(1, 0).Select
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Wend

Application.ScreenUpdating = True

End Sub

推荐答案

在您的行中:

In your line:

If c Is Nothing Then Exit Sub


您将退出子例程,而不处理输入框的空白变量.编写和"If"语句,以测试变量"c"的内容并相应地进行测试.

我不喜欢使用以下行,除非在编写VBA代码时绝对需要使用以下行:


You are exiting the sub routine without handling the blank variable for the input box. Write and "If" statement that tests the contents of the variable "c" and handle the test accordingly.

I don''t like to use the following line unless I absolutely have to when writing VBA code:

On Error Resume Next


当您使用此行时,您是说如果我遇到错误,请继续运行下一行代码,然后忽略该错误.在大多数情况下,这不是很好.而是编写一个错误处理程序例程来处理错误.

请注意:良好的VBA编程习惯可以在整个Internet上找到,尤其是在MSDN上(我知道,很难相信).

您不会在CP上找到很多VBA内容,因为该站点主要"处理C ++,C#和VB.Net编码.但是,我自己,Dalek Dave和其他人都有广泛的VBA背景,并且非常乐意为您提供帮助.

Dalek Dave是一位优秀的Excel VBA专家,也是我们的常驻会计师. 他的个人资料在这里 [


When you use this line then you are saying that if I get an error still run the next line of code and forget about the error. This is not good in most situations. Instead, write an error handler routine to handle the error.

Please note: Good VBA programming practices can be found all over the internet especially on MSDN (I know, hard to believe).

You will not find a lot of VBA stuff here at CP because this site "mainly" deals with C++, C#, and VB.Net coding. However, myself, Dalek Dave, and others have extensive VBA backgrounds and would be more than glad to help.

Dalek Dave, is a good Excel VBA guru and our resident accountant. His profile is here[^]

If you have any more questions then don''t hesitate to ask.

-- Good luck.


这篇关于EXCEL VBA:输入框中的错误处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆