My code was working 2 days back but now my excel just crashes. Thank for any help in Advance!

MrArchitAgarwal

New Member
Joined
Sep 9, 2023
Messages
5
Office Version
  1. 2021
Platform
  1. Windows
I am using 2 Worksheet_Change Events and the code was working great 2 days back but from yesterday the code stopped working and excel started crashing. Please Help.

This is the code I am using for the Worksheet

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

Dim xRInt As Integer
Dim xDStr As String
Dim xFStr As String
Dim xSStr As String
On Error Resume Next
xDStr = "G" 'Data Column
xFStr = "Y" 'Created Column
xSStr = "Z" 'Updated Column
If (Not Application.Intersect(Me.Range("G2:G"), Target) Is Nothing) Then
xRInt = Target.Row
If Me.Range(xFStr & xRInt) = "" Then
Me.Range(xFStr & xRInt) = Format(Now(), "mm/dd/yyyy hh:mm:ss")
End If
Me.Range(xSStr & xRInt) = Format(Now(), "mm/dd/yyyy hh:mm:ss")
End If



'_____________________________________________________________________________________________________________________________



Dim rng As Range
Dim cell As Range
Dim sizes() As String
Dim startSize As String
Dim endSize As String
Dim i As Integer
On Error Resume Next
' Define the range where the sizes should be generated
Set rng = Intersect(Target, Me.Range("J2:J"))

If Not rng Is Nothing Then
Application.EnableEvents = False

For Each cell In rng
If InStr(1, UCase(cell.Value), "-") > 0 Then
' Split the input into start and end sizes
sizes = Split(UCase(cell.Value), "-")
startSize = sizes(0)
endSize = sizes(1)

' Clear the original cell
cell.ClearContents

' Calculate and fill the cells downward with sizes
For i = 0 To 100 ' Increase this number to cover a larger range
cell.Offset(i, 0).Value = startSize
If UCase(startSize) = UCase(endSize) Then Exit For
startSize = GetNextSize(UCase(startSize))
Next i
End If
Next cell

Application.EnableEvents = True
End If
End Sub

Function GetNextSize(currentSize As String) As String
Select Case currentSize
Case "S"
GetNextSize = "M"
Case "M"
GetNextSize = "L"
Case "L"
GetNextSize = "XL"
Case "XL"
GetNextSize = "2XL"
Case "2XL"
GetNextSize = "3XL"
Case Else
GetNextSize = currentSize
End Select
End Function


SR.NO.BARCODEPARTY NAMEPARTY CODEITEM CODESTYLECOLOURSIZEQUANTITYPURCHASE PRICEPURCHASE VALUE% DISC FORWARDDISCOUNT IN RS.LANDING COSTWHOLESALERETAILMRPWHOLESALE NAMENAME ARONIUMPRICE ARONIUMSTATUS - ARONIUMSTATUS - BARCODEDate CreatedDate ModifiedMonth createdYear Created
1880401J. K. TEXTILESJK2781CO-ORD SETPINKL3179553850017952242804500CO-ORD SET 2781K224JCO-ORD SET 2781K224J - L PINK2700UPDATEDPRINTED
2880402J. K. TEXTILESJK2781CO-ORD SETPINKXL3179553850017952242804500CO-ORD SET 2781K224JCO-ORD SET 2781K224J - XL PINK2700UPDATEDPRINTED
3880403J. K. TEXTILESJK2782CROP TOP - DRAPEPINKL3179553850017952242804500CROP TOP - DRAPE 2782K224JCROP TOP - DRAPE 2782K224J - L PINK2700UPDATEDPRINTED
4880404J. K. TEXTILESJK2782CROP TOP - DRAPEPINKXL3179553850017952242804500CROP TOP - DRAPE 2782K224JCROP TOP - DRAPE 2782K224J - XL PINK2700UPDATEDPRINTED
5880405J. K. TEXTILESJK2783GOWNPINKM3169550850016952122704300GOWN 2783K212JGOWN 2783K212J - M PINK2600UPDATEDPRINTED
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
In the first macro add turn off and back on the EnableEvents in the positions shown below:
(You already have this covered in the 2nd macro)

Rich (BB code):
    If (Not Application.Intersect(Me.Range("G2:G"), Target) Is Nothing) Then
        Application.EnableEvents = False
        xRInt = Target.Row
        If Me.Range(xFStr & xRInt) = "" Then
            Me.Range(xFStr & xRInt) = Format(Now(), "mm/dd/yyyy hh:mm:ss")
        End If
        Me.Range(xSStr & xRInt) = Format(Now(), "mm/dd/yyyy hh:mm:ss")
        Application.EnableEvents = True
    End If
 
Upvote 0
Thanks, I tried that and it did work.

Now, any changes in any row is causing the first code to activate while the second code doesn't work at all. Can you please guide me on that to....

(P.S. I am a beginner in Excel)
 
Upvote 0
Firstly change the 2 intersect line to the code below:
(Your current code runs from G2:G and J2:J which doesn't work - it either needs the row number for the end cell or drop the row no and reference the whole column as in the code below)

Secondly your second macro looks for a dash "-" in column J but column J in your example is a number so it is never going to find a dash.
Should it be column S ?

Rich (BB code):
   ' Macro 1
    If Not Application.Intersect(Me.Range("G:G"), Target) Is Nothing Then
    ' Macro 2
    Set rng = Intersect(Target, Me.Range("J:J"))
 
Upvote 0
Firstly change the 2 intersect line to the code below:
(Your current code runs from G2:G and J2:J which doesn't work - it either needs the row number for the end cell or drop the row no and reference the whole column as in the code below)

Secondly your second macro looks for a dash "-" in column J but column J in your example is a number so it is never going to find a dash.
Should it be column S ?

Rich (BB code):
   ' Macro 1
    If Not Application.Intersect(Me.Range("G:G"), Target) Is Nothing Then
    ' Macro 2
    Set rng = Intersect(Target, Me.Range("J:J"))
No, The Row J is which contains Size, Due to Some hidden columns it isn't appearing correctly. Also Row G is Item Code. I have written the code in such a way that If i write S-XL in a cell in Column J, Then The cell will be filled with S, The lower cell will be filled with M, The further lower cell with L, then XL. But the code doesn't enter the loop. Please provide a solution, i couldn't -find one even after brainstorming for so much time :(

Also, the first code worked after I replaced
If (Not Application.Intersect(Me.Range("G2:G"), Target) Is Nothing) Then
with
If Not Intersect(Target, Me.Range("G2:G" & Me.Rows.Count)) Is Nothing Then
 
Upvote 0
Also, the first code worked after I replaced
If (Not Application.Intersect(Me.Range("G2:G"), Target) Is Nothing) Then
with
If Not Intersect(Target, Me.Range("G2:G" & Me.Rows.Count)) Is Nothing Then
We are going to get very far if you don't implement the suggestions provided.

1694244233384.png
 
Upvote 0
But if I change the header of Item Code, it will also change the header of Date Updated to the Date of Change...
 
Upvote 0
OK then try these 2:

Rich (BB code):
    If Not Intersect(Target, Me.Range("G2:G" & Me.Cells(Rows.Count, "G").End(xlUp).Row)) Is Nothing Then

    Set rng = Intersect(Target, Me.Range("J2:J" & Me.Cells(Rows.Count, "J").End(xlUp).Row))
 
Upvote 0
Solution

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top