Solved

VB in Excel

Posted on 2001-06-19
10
138 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now