简体   繁体   English

在清除工作表中的所有单元格后,不要运行Sub Worksheet_Change(ByVal Target As Range)

[英]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.

相关问题 将2个“ Private Sub Worksheet_Change(按目标的ByVal目标)”合并为1个 - Combining 2 “Private Sub Worksheet_Change(ByVal Target As Range)” into 1 从公共子调用(运行)私有子worksheet_Change(ByVal目标为范围) - Calling(run) a private Sub worksheet_Change(ByVal Target As Range) from public sub 在一个工作表中运行多个'Private Sub Worksheet_Change(ByVal Target As Range)' - Running multiple 'Private Sub Worksheet_Change(ByVal Target As Range)' in one worksheet 在我特别点击目标范围之前,Worksheet_Change(将目标作为范围)不会更改单元格 - Worksheet_Change(Byval target as range) does not change the cells until I click the target range spesifically Worksheet_Change(ByVal目标为范围),目标始终为空 - Worksheet_Change(ByVal Target As Range), Target always equals Nothing 难以在一个Excel工作表中合并2 x Private Sub Worksheet_Change(ByVal目标为范围) - Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet Worksheet_Change事件中的目标范围不正确 - Incorrect target range in Worksheet_Change event Worksheet_Change设置目标范围很慢 - Worksheet_Change setting target range is slow Worksheet_SelectionChange(ByVal目标为范围),目标中有两个单元格 - Worksheet_SelectionChange(ByVal Target As Range), Two Cells in Target Worksheet_Change(ByVal faixa As Range)在范围为空的情况下停止宏 - Worksheet_Change(ByVal faixa As Range) stop macro in case the range in empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM