If name range is not found then exit sub. Please help

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
I have the code below which works ok if name range if found but if name range is not found then it is giving error..

Please help.
Thanks
Pedie;)

Code:
Sub test21()
Dim r As String
Sheets("Sheet1").Select
   If Range(r) Is Nothing Then
     MsgBox "Name missing..."
     Exit Sub
     Else
     ThisWorkbook.Sheets("Data").Range("C2:C14").Copy
          Range(r).Select
           Do Until ActiveCell.Value = ""
            ActiveCell.Offset(1, 0).Select
             Loop
              Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
     xlNone, SkipBlanks:=False, Transpose:=True
             
            Application.CutCopyMode = False
          MsgBox "Ok"
          Application.ScreenUpdating = True
    End If
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try

Code:
Sub test21()
Dim r As String
r = ThisWorkbook.Sheets("Data").Range("C4").Value
Sheets("Sheet1").Select
   If Not NameExists(r) Then
     MsgBox "Name missing..."
     Exit Sub
     Else
     ThisWorkbook.Sheets("Data").Range("C2:C14").Copy
          Range(r).Select
           Do Until ActiveCell.Value = ""
            ActiveCell.Offset(1, 0).Select
             Loop
              Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
     xlNone, SkipBlanks:=False, Transpose:=True
             
            Application.CutCopyMode = False
          MsgBox "Ok"
          Application.ScreenUpdating = True
    End If
End Sub

Function NameExists(aName As String) As Boolean
Dim S As String
On Error GoTo EndFunction
S = ThisWorkbook.Names(aName).RefersTo
NameExists = True
Exit Function
EndFunction:
NameExists = False
End Function
 
Upvote 0
if I am using code to open another workbook(book2) and then look for name in that book2 will the code
change to ActiveWorkbook.Names(aName).RefersTo from Thisworkbook?

Code:
Function NameExists(aName As String) As Boolean
Dim S As String
On Error GoTo EndFunction
S = ActiveWorkbook.Names(aName).RefersTo
NameExists = True
Exit Function
EndFunction:
NameExists = False
End Function
 
Upvote 0
It will check for the existence of the named range in the active workbook - the one that you just opened.
 
Upvote 0
Peter, thank alot for helping & clearing things up...:)

Have a great day ahead!
 
Upvote 0

Forum statistics

Threads
1,216,174
Messages
6,129,296
Members
449,498
Latest member
Lee_ray

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