Revise Excel macro to reference a specific workbook tab name

I would like to revise the macro below so that it only runs in workbooks that have a tab called 'ncRptChangeForm'.

Sub Long_Form_Export()
'
' Long_Form_Export Macro
'

'
    Application.ActiveProtectedViewWindow.Edit
    Range("A10:A15").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Application.CutCopyMode = False
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").Select
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").Select
    Selection.ColumnWidth = 50
    Cells.Select
    Selection.AutoFilter
    Range("B2").Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("A1").Select
End Sub

Open in new window

Thanks!
Andrea
AndreamaryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Add this at the beginning of the sub

    Dim tmp As Worksheet
    On Error Resume Next
    Set tmp = ActiveSheet
    Sheets("ncRptChangeForm").Select
    tmp.Select
    If Err Then
        Stop
        If Err.Number = 9 Then Exit Sub
    End If
    On Error GoTo 0

Open in new window

0
DougCommented:
You could try this (pasted between lines 1 and 7 of your code):
Dim booRun As Boolean
Dim ws As Worksheet

booRun = False

For Each ws In ActiveWorkbook.Sheets
    If ws.Name = "ncRptChangeForm" Then
        booRun = True
        Exit For
    End If
Next ws

If Not booRun = True Then Exit Sub

Open in new window

0
AndreamaryAuthor Commented:
Thanks to you both for your solutions. Both appeared to work.

I did like the fact that Bugdrown's solution prevented the macro from running at all if the tab name didn't match 'ncRptChangeForm', whereas Syed's solution gave me a VBA error. Since many of our Excel users are novices, and may be confused by seeing the VBA window pop up, I prefer the former. That being said, I am not a programmer, so I don't know if there any other advantages / disadvantages between the two solutions? Is there someone that can outline this for me? It will also help me award points...
Thanks!
Andrea
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

DougCommented:
Andrea,
It sounds like you are looking for a moderator. You can request attention at the bottom of your original question.

I will say that one drawback from the code that I wrote is that the user has no feedback about why the code didn't run, if they were expecting it to. That could easily be changed with a friendly message (another line of code).

I'll step back until a moderator intervenes or until you or someone else has a question.
0
AndreamaryAuthor Commented:
Thanks, Bugdrown, good to know...I'll follow up with the moderator.
0
Saqib Husain, SyedEngineerCommented:
You do not need to request attention. You should select the one which better suits you.
0
DougCommented:
Thanks Syed.

Andrea,
I have to sign off but if my code works for you and you need a message with it, try pasting the following between lines 1 and 7 of your original code:

Dim booRun As Boolean
Dim ws As Worksheet

booRun = False

For Each ws In ActiveWorkbook.Sheets
    If ws.Name = "ncRptChangeForm" Then
        booRun = True
        Exit For
    End If
Next ws

If Not booRun = True Then
    MsgBox "The macro will run only in workbooks with a sheet named 'ncRptChangeForm'", vbOKOnly + vbInformation, "Attention"
    Exit Sub
Else
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndreamaryAuthor Commented:
Thanks again to you both. Doug, I appreciate you adding a message feature to the code!
0
DougCommented:
Glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.