Select Sheet Based on Variable Content VBA

Solved
Enito Posted messages 21 Status Member -  
Enito Posted messages 21 Status Member -
Hello,

I have several sheets in my Excel 2010 workbook. I'm working in VBA.
I have a Variant variable that contains a number, and one of the sheets in the workbook is named after that number, so I would like to be able to select the sheet based on the variable. For example,

Sub Select_Sheet ()
Sheets(TOTO).Select
End Sub

Obviously, TOTO is defined, but I'm always getting the error message that the index does not belong to the selection.

Can someone help me with this?

Thanks in advance!

ENITO!

Configuration: Windows Vista / Firefox 10.0.1

2 answers

eriiic Posted messages 24581 Registration date   Status Contributor Last intervention   7 281
 
Hello,

Dim toto As Long toto = 2 Worksheets(toto).Select
works very well.

Ensure that your variable is visible to your procedure (declare it outside of any sub if it is initialized in another sub), and that there are no unwanted spaces in your sheet names.

eric
0
Enito Posted messages 21 Status Member
 
Thank you for this response. Subsequently, I realized that my variable TOTO was being interpreted as a sheet index when I used Sheets(TOTO).select

Thank you and have a nice day,

Enito!
0