Click here to Skip to main content
Sign Up to vote bad
good
See more: Excel
How to use this below code ?
i want to select case
 

 
Sub SelectedWoksheets()
'http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm
Dim ws As Worksheet
 
    For Each ws In ActiveWindow.SelectedSheets
        With ws
        
            'With Code Here
        
        End With
    Next ws
End Sub
 
[Edit]Code block added[/Edit]
Posted 3 Nov '12 - 5:33
tcchean308
Edited 3 Nov '12 - 5:50
ProgramFOX56.3K

Comments
Richard MacCutchan - 3 Nov '12 - 12:18
What do you mean you want to select case? In order to do what exactly? Please edit your question and explain what you are trying to do.
ridoy - 3 Nov '12 - 16:20
totally unclear,elaborate it..

1 solution

If i understand you well, you want to use Select Case ... End Select statement.
Select case ws.Name
    Case "Sheet1"
        'do something
    Case "Sheet2"
        'do something
    Case "Sheet3"
        'do something
    Case Else
        'do something
End Select
 
But if would like to go through the collection of worksheets to "find" the correct one by its name, better use something like this:
Function GetWoksheet(wshName As String, wbk As Workbook) As Worksheet
Dim wsh As Worksheet
 
On Error Goto Err_GetWoksheet
 
Set wsh = wbk.Workseets(wshName)
 
Exit_GetWoksheet:
    Set GetWoksheet = wsh
    Exit Function
 
Err_GetWoksheet:
    MsgBoxErr.Description, vbExclamation, Err.Number
    Set wsh = Nothing
    Resume Exit_GetWoksheet
 
End Function
 
Usage:
Set wsh =  GetWoksheet("Sheet4", ActiveWorkbook)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 253
2 OriginalGriff 190
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid