Event change error if different sheet activated

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
I have been using the below macro to automatically add a semi colon when time is entered and it works great, but today I it errored. if I activate a cell within my named range "Time" and set the focus within the cell with the cursor and before I change the focus to a new cell I select another sheet within the workbook the macro bugs stating, "Method intersect of object_Global failed" Can anyone tell me what I could add to avoid this error? Thank you.

Code:
Sub EnterTime(Target)
    If Target.Cells.CountLarge > 1 Then Exit Sub
    If Target.Value = "" Then Exit Sub
    If Intersect(Target, Range("Time" & ActiveSheet.Index)) Is Nothing Then Exit Sub
    Application.EnableEvents = False
        With Target
            If Not .HasFormula And IsNumeric(.Value) And .Value < 2400 Then
                .Value = TimeValue(Format(.Value, "00:00"))
            Else
                .Value = ""
                With ErrMsg
                    .Label1.Caption = "Error:" & vbNewLine _
                    & "The time entered was not valid"
                    .Show
                End With
            End If
        End With
    Application.EnableEvents = True
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
sorry this is where the procedure is called

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Call EnterTime(Target)
End Sub
 
Upvote 0
also I have named ranges Time1, Time2, and Time3 all relating to sheets(1), sheets(2), and sheets(3). so the only reason I use the activesheet.index is to refer to the correct named range based on what sheet is active. Im not sure if that needed to be specified or not
 
Upvote 0
yes, I just did and that did work, now I just need to incorporate that with my other 2 event changes that are called, thank you!
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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