Quick "if not found" code request

zombiemaster

Board Regular
Joined
Oct 27, 2009
Messages
245
Here's an easy one (for those of you who are not ME, that is)...

I have a multi-tab workbook that includes a MASTER sheet as well as (usually) a sheet named "UNKNOWN". My code copies some data from MASTER to UNKNOWN...BUT...what happens if "UNKNOWN" isn't there? I'm sure it will crash because it can't find it, and although I've been lucky so far and it's there every time, there's no guarantee...

So - here's my existing code:

Code:
    Sheets("MASTER").Select
    Range("F1:H1").Select
    Selection.Copy
    Sheets("UNKNOWN").Select
    Range("F1").Select
    ActiveSheet.Paste

I want the code to ignore that piece if UNKNOWN isn't found as a tab name. Any thoughts on an easy simple fix?

Thanks as always,
~ZM~
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this:
Code:
Sub Copy_Me()
On Error GoTo M
'Modified  4/25/2019  11:40:29 AM  EDT
Sheets("MASTER").Range("F1:H1").Copy
Sheets("UNKNOWN").Range("F1").PasteSpecial
Application.CutCopyMode = False
Exit Sub
M:
Application.CutCopyMode = False
MsgBox "We had a problem here"
End Sub
 
Upvote 0
How about
Code:
If Evaluate("isref(UNKNOWN!A1)") Then
   Sheets("MASTER").Range("F1:H1").Copy Sheets("UNKNOWN").Range("F1")
End If
 
Upvote 0
Hi,
not as clean as Fluff's but i included some error handling in case copy / paste fails

Code:
Const SheetName As String = "UNKNOWN"
    On Error GoTo exitsub
    If Evaluate("ISREF('" & SheetName & "'!A1)") Then
        Sheets("MASTER").Range("F1:H1").Copy Sheets("UNKNOWN").Range("F1")
    Else
        Err.Raise 600, , "Sheet Name: " & SheetName & Chr(10) & "Not Found"
    End If
exitsub:
    If Err > 0 Then MsgBox (Error(Err)), 48, "Error"

Dave
 
Last edited:
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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