Solved

VB in Excel

Posted on 2001-06-19
10
139 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now