[英]Don't run Sub Worksheet_Change(ByVal Target As Range) after all cells in the worksheet has been cleared
I have a macro where i import a text file and update some elements of this file using the macro and then re-create the text file with the updated elements. 我有一个宏,在其中导入文本文件并使用该宏更新此文件的某些元素,然后使用更新的元素重新创建文本文件。 I am validating some of the cells in a particular worksheet (USERSHEET)to make sure the user entries are correct and using the below Sub:
我正在验证特定工作表(USERSHEET)中的某些单元格,以确保用户输入正确并使用下面的Sub:
Option Explicit
Public Rec_Cnt As Integer
Private Sub Worksheet_Change(ByVal Target As Range)
Rec_Cnt = Sheets("MD").Cells(3, 7)
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Set Rng1 = Range("E2:E" & Rec_Cnt)
Set Rng2 = Range("K2:K" & Rec_Cnt)
Set Rng3 = Range("Q2:Q" & Rec_Cnt)
If Not Application.Intersect(Target, Rng1) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Ticket_Error
Exit Sub
End If
ElseIf Not Application.Intersect(Target, Rng2) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Cnj_Ticket_Error
Exit Sub
End If
ElseIf Not Application.Intersect(Target, Rng3) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Ticket_Error
Exit Sub
End If
End If
End Sub
Sub Original_Ticket_Error()
MsgBox "Original Ticket Number is more 10 characters"
End Sub
Sub Original_Cnj_Ticket_Error()
MsgBox "Original Conj. Ticket Number is more 10 characters"
End Sub
=============================================================================== Once the text file is created with the updated columns I am clearing all the cells in the USERSHEET. ================================================== =============================一旦使用更新的列创建了文本文件,我将清除USERSHEET中的所有单元格。 However, I get a run-time error '13' for type mismatch
但是,我收到类型不匹配的运行时错误“ 13”
I wanted to check how can I avoid calling the Private Sub Worksheet_Change(ByVal Target As Range) after the worksheet(USERSHEET) is cleared 我想检查一下如何清除工作表(USERSHEET)后避免调用Private Sub Worksheet_Change(ByVal Target As Range)
Any help is much appreciated. 任何帮助深表感谢。
Thanks, sachin 谢谢,sachin
Edit: 编辑:
Code used to clear usersheet: 用于清除用户表的代码:
Sub Clear_User_Sheet()
Sheets("UserSheet").Select
Range("A2:R100002").Select
Application.Wait (Now + TimeValue("0:00:01"))
Selection.Delete
Application.EnableEvents = False
Application.Wait (Now + TimeValue("0:00:01"))
Selection.Delete
Selection.Delete
Sheets("Control Panel").Select
End Sub
Try this version of Clear_User_Sheet
instead: 请尝试使用此版本的
Clear_User_Sheet
:
Sub Clear_User_Sheet()
Application.EnableEvents = False
Sheets("UserSheet").Range("A2:R100002").Delete
Application.EnableEvents = True
End Sub
PS. PS。 If you've used the code that you suggested in your edited answer, you may well find that
EnableEvents
is currently set to False
- you'll want to correct that before running anything else. 如果你使用的是你在编辑答案提出的代码,你可能会发现,
EnableEvents
目前设定为False
-你要纠正运行之前别的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.