Link to home
Start Free TrialLog in
Avatar of "Abys" Wallace
"Abys" WallaceFlag for United States of America

asked on

Excel VBA- Only Select Worksheets Containing a Comma

Hi Experts :)

Quick question... Is there a way to select only worksheet's whose name contains a comma?

If so what would be the VBA code for it?

I would like all worksheets containing commas selected.  They will all be next to each also, just FYI if that would make a difference.

Thank you for your assistance!
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

select them to do what
Would the following Macro help you?

Sub activateSheet(sheetname As String)
'activates sheet of specific name
    Worksheets(sheetname).Activate
End Sub

Basically you want to make use of the .Activate function. Or you can use the .Select function like so:

Sub activateSheet(sheetname As String)
'selects sheet of specific name
    Sheets(sheetname).Select
End Sub
try this
send this function a comma


Sub activateSheet(sheetname As String)
DIM Sheetfound = ""
 For Each sheet In ActiveWorkbook.Sheets
    If sheet.Name Like "*" & sheetname & "*" Then
       Sheetfound = sheet.Name
         EXIT FOR
    End If
 Next
    Worksheets(sheetfound).Activate
End Sub
Sub selector()
    Dim comp1() As String, ws As Worksheet
 
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "*,*" Then
            Worksheets(ws.Name).Select (False)
        End If
    Next ws
    
End Sub

Open in new window


Try above
Avatar of "Abys" Wallace

ASKER

Hi esskayb2d...  Thank you for your help!

I attempted to use the latter code but it's not functioning.  I attached a sample workbook to assist.  

I placed your code in a module named:  modSelectWS and attempted to Step through the code but the "DIM Sheetfound = "" is in RED and the remaining code advises I'm unable to rename a sheet the same as one that already exists.

I was selecting the sheet because I want to copy the range A3:F5 from the "Email Daily Stats" sheet onto each sheet that has a name on it with the following format:  "Last Name, First Name" in that sheets A1:F3 range.. cell A3 should contain the worksheet's name

I was thinking selecting each sheet with a comma would be the easiest as the Master workbook contains over 20 sheets outside of the ones with the employee names.

Thank you again ~
SampleDeleteWSBasedCriteria.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Shanan212
Shanan212
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Shanan212:
You came through again, appreciate!  :)

Kindest Regards