Solved

VB in Excel

Posted on 2001-06-19
10
142 Views
Last Modified: 2012-05-04
hi, two questions but first i post some code

; activate worksheet 1 at open of the workbook
Private Sub Workbook_Open()
    Worksheets(1).Select
End Sub
;in sheet1 i read all the worksheets of the workbook and put them in a selectionlist
on click in the selectionlist the approriate page is displayed

Private Sub CmbWerkbladen_Click()
Dim ind As Integer
   
    ind = CmbWerkbladen.ListIndex + 1
    Worksheets(ind).Activate

End Sub

Private Sub Worksheet_Activate()
Dim objN As Object
Dim i As Integer

    CmbWerkbladen.Clear
    For Each objN In Sheets
        i = i + 1
        CmbWerkbladen.AddItem objN.Name
    Next
   
   
    CmbWerkbladen.Text = CmbWerkbladen.List(0)
    CmbWerkbladen.Select
End Sub

1/ Sometimes the selection list isn't filled up at opening of the workbook, but when i manually choose the first sheet (again), it works.
2/ that is not my main problem though, in visual you can give a property sorted to the combobox or selection list
in Excel you can't (at least i don't know how)
How can i easily sort the worksheets in the selection list ?

thxs
McRight
0
Comment
Question by:McRight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6207533
First, add this:

Private Sub Workbook_Open()
   Worksheets(1).Select
   Worksheets(1).Activate
End Sub

and change this lines:

Private Sub Worksheet_Activate()
Dim objN As worksheet

   CmbWerkbladen.Clear
   For Each objN In Sheets
       CmbWerkbladen.AddItem objN.Name
   Next
   
 
   CmbWerkbladen.Text = CmbWerkbladen.List(0)
   CmbWerkbladen.Select
End Sub


0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6207579
Second, It could be posible save the all list of worksheets on a range, sort it and use it as Linkedcell property.
0
 
LVL 1

Author Comment

by:McRight
ID: 6207624
thxs,
so the things i have to change are :
removing the i variable (since it isn't used)
and define the object as worksheet
but can you give me a small hint in how i can sort the range and use linkedcell
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6208436
Try this,
suppose you saved the enum of worsheets starting on range Z1:
..........
Dim rng As Range
  Set rng = Range("Z1", Range("Z1").End(xlDown))
  rng.Select
  rng.Sort order1:=xlAscending
  CmbWerkbladen.LinkedCell = rng
 
  CmbWerkbladen.Text = CmbWerkbladen.List(0)
  CmbWerkbladen.Select
End sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6208492
optionally, you don't need to use a combobox, you could use data validation:

Select the cell where will appear the list, go to menu Data->Validation... (Sorry, i have excel in spanish;), from the combo select List... blah, blah.

This code does it for you:
Suposse you will have the list on cell C6:

Range("C6").Select

    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=$Z$1:$Z$5"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With


Hope it helps.
0
 
LVL 1

Author Comment

by:McRight
ID: 6211664
i tried your first solution :
rng.Sort order1:=xlAscending
CmbWerkbladen.LinkedCell = rng
but on both lines he gives an error
 
is there another way ?
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6211715
Ouch, i am so sorry, the property is listfillrange.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139516
Hi McRight,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept Richie_Simonetti's comment(s) as an answer.

McRight, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Author Comment

by:McRight
ID: 7141090
Question was not properly answered but you may close this one
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7141247
Hi McRight,
Do you object to giving the points to Richie_Simonetti?  Or do you have some other idea for closing this question?  You can close it yourself by clicking the [Accect Comment as Answer] near Richie_Simonetti's comment.  -- Dan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question