Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate a specific worksheet in Excel?

Tags:

excel

vba

I just need to activate a certain worksheet. I have a string variable that keeps the name of the worksheet.

like image 513
Alex Avatar asked Oct 25 '10 10:10

Alex


2 Answers

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
like image 68
Dennis G Avatar answered Sep 22 '22 00:09

Dennis G


I would recommend you to use worksheet's index instead of using worksheet's name, in this way you can also loop through sheets "dynamically"

for i=1 to thisworkbook.sheets.count
 sheets(i).activate
'You can add more code 
with activesheet
 'Code...
end with
next i

It will also, improve performance.

like image 25
Moreno Avatar answered Sep 21 '22 00:09

Moreno