subscript out of range when try write wrong sheet name in cell

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
I want fixing this error subscript ou of range when write error sheet name in cell A2
my goal if the sheet name is wrong then should populate MsgBox "please check the right sheet name"
and if sheet name is right then should activate sheet .
here is my code
VBA Code:
Sub activate_sh()
Dim ws As Variant
For Each ws In Array("a", "b", "d")
If Worksheets(Worksheets("ACTIVATE").Range("A2").Text) <> ws.Name Then
MsgBox "please check  the  right sheet name"
Else
Worksheets(Worksheets("ACTIVATE").Range("A2").Text).Activate
End If
Next
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about
VBA Code:
Sub abdelfattah()
   Dim Ary As Variant, Chk As Variant
   
   Ary = Array("a", "b", "d")
   With Worksheets("ACTIVATE").Range("A2")
      Chk = Application.Match(.Text, Ary, 0)
      If IsError(Chk) Then
         MsgBox "please check  the  right sheet name"
      Else
         Worksheets(.Text).Activate
      End If
   End With
End Sub
 
Upvote 0
Solution
that's awesome , could do that by sheet change event instead of button,please?
 
Upvote 0
Do you just want it to work on A2?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Ary As Variant, Chk As Variant
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "A2" Then
      Ary = Array("a", "b", "d")
      Chk = Application.Match(Target.Value, Ary, 0)
      If IsError(Chk) Then
         MsgBox "please check  the  right sheet name"
      Else
         Worksheets(Target.Value).Activate
      End If
   End If
End Sub
 
Upvote 0
thanks I want clearing A2 after show message . I try adding as bold line but the message shows many times .
Rich (BB code):
MsgBox "please check  the  right sheet name"
        Target.Value = "": Exit Sub
 
Upvote 0
Any thing else that you "forgot" to ask initially?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Ary As Variant, Chk As Variant

   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "A2" Then
      Ary = Array("a", "b", "d")
      Chk = Application.Match(Target.Value, Ary, 0)
      If IsError(Chk) Then
         MsgBox "please check  the  right sheet name"
      Else
         Worksheets(Target.Value).Activate
         Application.EnableEvents = False
         Target.Value = ""
         Application.EnableEvents = True
      End If
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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