Solved

VB in Excel

Posted on 2001-06-19
10
141 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft Access combo box help 2 49
Add a task in Outlook from access 11 42
Hide vba in gp 7 104
VBA: loop recent folder and copy txt file. 8 34
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

856 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