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:
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. However, I get a run-time error '13' for type mismatch
I wanted to check how can I avoid calling the Private Sub Worksheet_Change(ByVal Target As Range) after the worksheet(USERSHEET) is cleared
Any help is much appreciated.
Thanks, 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:
Sub Clear_User_Sheet()
Application.EnableEvents = False
Sheets("UserSheet").Range("A2:R100002").Delete
Application.EnableEvents = True
End Sub
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.