View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default can't select worksheet

Your posting showed worksheets(6), what your are really getting is
worksheets("6"). You need to convert the string to a number like
worksheets(val(trim("6")))

"donbowyer" wrote:

Hi Joel
Thanks for the reply.
However, from my ListBox, MyText is an alpha string with no numbers, for
example <<UnitedAirlines.
So it is the Worksheet entitled <<UnitedAirlines that I want to Select,
but as I say, Application.ActiveWorkbook.Worksheets(MyText).Sele ct doesn't
work, even though as you suggest, Worksheets() can take a string as well as a
number.

--
donwb


"Joel" wrote:

Worksheets() can either take a string or a number. when its a nuber it is
the count of which worksheet.

Whey ou use Worksheets(6) it is a number indicating the 6th worksheet

Listboxes contain strings that must be converted to numbers.

change from
MyText = ListBox1.Text
to
MyNumber = val(trim(ListBox1.Text))

The change
Application.ActiveWorkbook.Worksheets(MyText).Sele ct
to
Application.ActiveWorkbook.Worksheets(MyNumber).Se lect


"donbowyer" wrote:

The code below is in a userform.
MyText appears in the correct format.
However, when run, I get the <<error9, out of range error message
If I substiute ...Worksheets(MyText)... with ...Worksheets(6)... it runs.
What have I done wrong??

Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As
Integer, _
ByVal X As Single, ByVal Y As Single)
MyText = ListBox1.Text
Application.ActiveWorkbook.Worksheets(MyText).Sele ct
Unload UserForm1
End Sub
--
donwb