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
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
What is calling that procedure?

You could try replacing:

ActiveSheet.Index

with:

Target.Parent.Index
 

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
sorry this is where the procedure is called

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Call EnterTime(Target)
End Sub
 

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
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
 

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,385
Messages
5,601,331
Members
414,446
Latest member
CRAVIN

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
Top