简体   繁体   中英

Workbook_sheetChange crash when cell is open and another worksheet is selected

I am working on an Excel file for operator on a machine, so it need to be armored.

I am running on a small bug link to a specific scenario:

When i edit a cell from the specified range from my Workbook_sheetChange and click on another sheet the code crash and give a 1004, i know why (wrong objet since i produce a cross-sheet _Global link) but i dont know how to avoid it. I looked trough Stack and google but didnt found anything, also i have an mec engereeing programming course so i dont know a lot about object and event.

Heres my code:

Sub Workbook_sheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Range("C4:C8")) Is Nothing Then 'Si changement dans les cellules "nom employé"

Call Employe ' Appel Macro pour nom automatique

End If
If Not Intersect(Target, Range("U18:V42")) Is Nothing Then

Call tritroughsheets ' Macro tri des temps d'arret

End If

End Sub

In this case for exemple if C4 is in edit mode and in Sheet1 and the operator click on Sheet2 by mistake it'll pop-up an error message (which we dont want operator to deal with)

Im looking for lines like

If(cell is being edited) Then
dont run my stuff till its close
End if

Any idea?

Thx, (wear mask boïs)

Try changing of

If Not Intersect(Target, Range("C4:C8")) Is Nothing

with:

If Not Intersect(Target, sh.Range("C4:C8")) Is Nothing 'exactly qualifies/references the range to the sh worksheet...

VBA tries an intersection between ranges in two different worksheets. When remained in the worksheet, Range("C4:C8") refers to the (correct) active sheet . When you move from a cell to another worksheet, without changing anything in the analyzed range , the event is not triggered... So, not the cell editing is the problem.

It is difficult to locate bugs without seeing the subroutines being called. I would suggest disabling and re-enabling events while your sub is running:

Sub Workbook_sheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.EnableEvents = False
        
        If Not Intersect(Target, Range("C4:C8")) Is Nothing Then 'Si changement dans les cellules "nom employé"
            Call Employe ' Appel Macro pour nom automatique
        End If
        
        If Not Intersect(Target, Range("U18:V42")) Is Nothing Then
            Call tritroughsheets ' Macro tri des temps d'arret
        End If
        
    Application.EnableEvents = True
End Sub

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM