Array comparison, current sheets in workbook vs array list

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
I am trying to create almost a "sheet validate" code in which it will cycle through the current workbooks sheets to make sure it matches to the defined array.
I have the initial part of the code where it takes all the sheets in the current workbook and puts them in an array. My problem lies around the Application.Match when comparing it to the defined array or hardcoded array.
I am using CodeNames for the sheets to make it more accurate if a user deletes a sheet, inserts a new one and renames it the same as the deleted.

All I am really looking for is to give me a message box of the missing value from the defined array if it doesn't exist in the current workbook.
I am also avoiding any writing of values in the workbook.

Code:
Sub ArraySheets()    
Dim SheetNames() As Variant
    Dim i As Long
    Dim a As String
        Dim iArray() As Variant
            iArray = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9", "Sheet10", "Sheet11", "Sheet13", "Sheet14", "Sheet15", "Sheet16", "Sheet17", "Sheet18", "Sheet20", "Sheet21", "Sheet22", "Sheet26")
    Dim TotalArray As Variant
    
        For i = 1 To Sheets.Count
            a = Sheets(i).CodeName
                ReDim Preserve SheetNames(1 To i)
                    SheetNames(i) = a
            a = ""
        Next i
            For i = LBound(SheetNames) To UBound(SheetNames)
                If Error(Application.Match(SheetNames(i), iArray, 0)) Then
                    TotalArray = SheetNames(i) & vbNewLine
                End If
            Next i
            MsgBox (TotalArray)
end sub
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Unfortunately there is a slight flaw in your plan.
Namely Xl allocates codenames the same way it allocates sheet names. So if when you open the workbook there is no codename sheet1 & you add a sheet that will be codename sheet1.
Therefore you will need to rename the codenames. If you do that by adding cn to the end of the codename like Sheet1cn you could filter the array, which would be the easiest way to get what you want.
 
Upvote 0
this is not what I am asking; I have sheets with the with the highest allocated number i.e. - Sheet29 and Sheet30 with visibility set to xlveryhidden, therefore what you are describing will not occur. What I am asking is if it is possible to compare a defined array to a variable array.
 
Upvote 0
Pardon me for breathing.
But, unless everyone of the sheets in you array is veryhidden (unlikely, bearing in mind what you said in your op) then you could have the problem I mentioned.
That said, the answer to your question is yes.
Try
Code:
If [COLOR=#ff0000]Is[/COLOR]Error(Application.Match(SheetNames(i), iArray, 0)) Then
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,276
Members
449,149
Latest member
mwdbActuary

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