VBA to check if Sheet name doesnt exists from single array

mhdakhtar8

New Member
Joined
Feb 4, 2013
Messages
36
Hi,

I'm in a situation where I need to check if some sheets already exist in a workbook which I need to run a macro on. That macro is designed for selected sheets. If any of the sheet is not existing, it will fail.

I want to put a control for VBA to check whether those sheets already exists or not.

Here's what I've reached to so far but It's giving me Compile error for sname.Length. The condition is I only want sheet name to be checked from Array for existence.

Code:
Sub Generate_Producitvity_Report()
Dim OW As Workbook, ws As Worksheet, sname() As String, i As Integer
sname = Array("COS REJECT", "APPROVAL SENT", "Pending Client Response", "Awaiting Four-Eye Check", "Awaiting RDS Approval", "SHEET6", "SHEET1")
Set OW = ActiveWorkbook
For Each ws In ThisWorkbook.Worksheets
ws.Select
With ws
    [COLOR="#FF0000"]For i = 0 To sname.Length[/COLOR]
        If ws.Name <> sname(i) Then
        MsgBox sname(i) & " sheet not found"
        End If
    Next i
End With
Next ws
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:
Code:
Sub Generate_Producitvity_Report()
    Dim OW As Workbook, ws As Worksheet, sname As Variant, i As Integer
    sname = Array("COS REJECT", "APPROVAL SENT", "Pending Client Response", _
        "Awaiting Four-Eye Check", "Awaiting RDS Approval", "SHEET6", "SHEET1")
    Set OW = ActiveWorkbook
    
    For Each sN In sname
        On Error Resume Next
        Set ws = ThisWorkbook.Sheets(sN)
        If Err.Number > 0 Then Debug.Print sN & "sheet not found": Err.Clear
        On Error GoTo 0
    Next sN
End Sub
 
Upvote 0
As far as I know, if you use String, you'll have to initialise all the posts one by one.
Declare it as Variant instead.
Also, your line in red, try using Ubound(sname) instead.
 
Upvote 0
Try this:
Code:
Sub Generate_Producitvity_Report()
Dim ws As Worksheet, i As Integer


    sname = Array("COS REJECT", "APPROVAL SENT", "Pending Client Response", "Awaiting Four-Eye Check", "Awaiting RDS Approval", "SHEET6", "SHEET1")


    On Error GoTo NotFound:


    For i = 0 To UBound(sname)
        Set ws = ActiveWorkbook.Sheets(sname(i))
    Next i
    Exit Sub


NotFound:
    MsgBox sname(i) & " sheet not found"
End Sub
 
Upvote 0
Thanks, its working without declaring. But I need to declare the
Code:
sN
. What should I declare it as?

Code:
For Each sN In sname
 
Upvote 0
Thanks @V_Malkoti, its working without declaring. But I need to declare the "sN". What should I declare it as?

Code:
For Each sN In sname
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,963
Members
449,412
Latest member
montand

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