Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB in Excel

Posted on 2001-06-19
10
Medium Priority
?
147 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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