[vba] Checking if sheet exists inside array

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have this sub from @jmacleary found on this post
but i thought i would be best to start a new post as the problem has shifted
So the sub will grab sheets from an array and put the first column of each sheet into one sheet titled "Unknown"
My problem is that the sheets are now generated based on user needs, so there may not be all 6 sheets depicted in the array.
I feel mega dumb for not being able to write a code that says if this sheetname exists continue with the code, else do nothing?
I can't get a custom function that uses boolean to work or anything.
halp

Code:
Sub GetColumnA()
'

'
Dim lastRow As Long
Dim SheetNames As Variant
SheetNames = Array("MTH", "WP", "MKK", "TTW", "W1", "RHH")
For Each sheetname In SheetNames

lastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
    Sheets(sheetname).Range("A2:A" & lastRow).Copy
destrow = Sheets("Unknown").Range("A" & Rows.Count).End(xlUp).Row + 1
    Sheets("Unknown").Select
    Range("A" & destrow).Select
    ActiveSheet.Paste
Next sheetname
    
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Here's a function you can invoke to determine if a sheet exists.

Code:
Function SheetExists(shName As String) As Boolean
SheetExists = False
For Each sh In Worksheets
    If sh.Name = shName Then
        SheetExists = True
        Exit For
    End If
 Next sh
End Function
You can call it like this:
Code:
........
SheetNames = Array("MTH", "WP", "MKK", "TTW", "W1", "RHH")
For Each sheetname In SheetNames 
      If SheetExists(sheetname) Then
          'rest of code
 
Last edited:
Upvote 0
How about
Code:
Sub GetColumnA()
'

'
Dim lastRow As Long
Dim SheetNames As Variant
SheetNames = Array("MTH", "WP", "MKK", "TTW", "W1", "RHH")
For Each sheetname In SheetNames
If Evaluate("isref('" & sheetname & "'!A1)") Then
   lastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
       Sheets(sheetname).Range("A2:A" & lastRow).Copy
   destrow = Sheets("Unknown").Range("A" & Rows.Count).End(xlUp).Row + 1
       Sheets("Unknown").Select
       Range("A" & destrow).Select
       ActiveSheet.Paste
End If
Next sheetname
    
End Sub
 
Upvote 0
Here's a function you can invoke

this i tried already. I get a ByRef arguement type mismatch on "sheetname" in
Code:
If SheetExists(sheetname) Then
possibly because sheetnames as variant?
 
Upvote 0
You're welcome & thanks for the feedback.

If you try
Code:
If SheetExists(CStr(sheetname)) Then
That should sort the problem.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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