Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
PFS PFS is offline
external usenet poster
 
Posts: 8
Default Pre-select visible sheets in listbox

Hi All,

I have a user form which has a listbox which lists all the sheets in
the workbook.

It allows the user to pick from the list which sheets they would like
to see, and hide the unselected ones.

What I would like the listbox to do when the form opens, is to pre-
select the sheets that are already visible, so that the user can just
modify the selction instead of starting from scratch.

Any help that people can give would be appreciated

cheers
Paul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Pre-select visible sheets in listbox

As a user, I would think that it would make more sense to ask me to select the
sheets to hide--but that doesn't change how the listbox would be initialized.

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()

Dim iCtr As Long

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
On Error Resume Next
'xlsheetveryhidden ????
ActiveWorkbook.Sheets(.List(iCtr)).Visible = xlSheetHidden
If Err.Number < 0 Then
Err.Clear
MsgBox "Error while trying to hide: " & .List(iCtr)
End If
On Error GoTo 0
End If
Next iCtr
End With

End Sub
Private Sub UserForm_Initialize()

Dim sh As Object 'could be any type of sheet

For Each sh In ActiveWorkbook.Sheets
If sh.Visible = xlSheetVisible Then
Me.ListBox1.AddItem sh.Name
End If
Next sh

End Sub


PFS wrote:

Hi All,

I have a user form which has a listbox which lists all the sheets in
the workbook.

It allows the user to pick from the list which sheets they would like
to see, and hide the unselected ones.

What I would like the listbox to do when the form opens, is to pre-
select the sheets that are already visible, so that the user can just
modify the selction instead of starting from scratch.

Any help that people can give would be appreciated

cheers
Paul


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
PFS PFS is offline
external usenet poster
 
Posts: 8
Default Pre-select visible sheets in listbox

Thanks Dave,

I should have said, my listbox lists "all" sheets in the workbook
(visible and hidden) and the chosen selection is likely to be much
smaller than the "hidden" list.

The list is to allow a farmer to select what crops he has on his
farm. We will have around 30-40 different crop templates available to
choose from, but a farmer is only likely to choose up to 10

When he goes to add a new crop to his farm or stops growing a crop, I
would like his existing crops (sheets) to be already selected in the
listbox, so that he can modify his selection easier, rather than
having to select everything that he already grows from scratch each
time (because in my code for the action button, what is selected in
the listbox is made visible, and the rest are hidden)

Hope this makes sense. (and my apologies if the above does this
already, I am relatively new to VBA and forms)

cheers
Paul
  #4   Report Post  
Posted to microsoft.public.excel.programming
PFS PFS is offline
external usenet poster
 
Posts: 8
Default Pre-select visible sheets in listbox

On Nov 30, 12:52*pm, PFS wrote:
Thanks Dave,

I should have said, my listbox lists "all" sheets in the workbook
(visible and hidden) and the chosen selection is likely to be much
smaller than the "hidden" list.

The list is to allow a farmer to select what crops he has on his
farm. *We will have around 30-40 different crop templates available to
choose from, but a farmer is only likely to choose up to 10

When he goes to add a new crop to his farm or stops growing a crop, I
would like his existing crops (sheets) to be already selected in the
listbox, so that he can modify his selection easier, rather than
having to select everything that he already grows from scratch each
time (because in my code for the action button, what is selected in
the listbox is made visible, and the rest are hidden)

Hope this makes sense. (and my apologies if the above does this
already, I am relatively new to VBA and forms)

cheers
Paul


In short I need some code to do....

If a sheet is visible, have it pre-selected in the listbox as the form
opens

cheers
Paul
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Pre-select visible sheets in listbox

You could use something like:

Private Sub UserForm_Initialize()

Dim sh As Object 'could be any type of sheet

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
For Each sh In ActiveWorkbook.Sheets
.AddItem sh.Name
If sh.Visible = xlSheetVisible Then
.Selected(.ListCount - 1) = True
End If
Next sh
End With
End Sub

I'm not quite sure what you're doing, but it may be easier to create two
listboxes and have some buttons that "move" the entries from one listbox to the
second and removes it from the first.

It may be easier for the user to see what's been selected instead of scrolling a
long listbox????

PFS wrote:

On Nov 30, 12:52 pm, PFS wrote:
Thanks Dave,

I should have said, my listbox lists "all" sheets in the workbook
(visible and hidden) and the chosen selection is likely to be much
smaller than the "hidden" list.

The list is to allow a farmer to select what crops he has on his
farm. We will have around 30-40 different crop templates available to
choose from, but a farmer is only likely to choose up to 10

When he goes to add a new crop to his farm or stops growing a crop, I
would like his existing crops (sheets) to be already selected in the
listbox, so that he can modify his selection easier, rather than
having to select everything that he already grows from scratch each
time (because in my code for the action button, what is selected in
the listbox is made visible, and the rest are hidden)

Hope this makes sense. (and my apologies if the above does this
already, I am relatively new to VBA and forms)

cheers
Paul


In short I need some code to do....

If a sheet is visible, have it pre-selected in the listbox as the form
opens

cheers
Paul


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
PFS PFS is offline
external usenet poster
 
Posts: 8
Default Pre-select visible sheets in listbox

On Nov 30, 11:29*pm, Dave Peterson wrote:

I'm not quite sure what you're doing, but it may be easier to create two
listboxes and have some buttons that "move" the entries from one listbox to the
second and removes it from the first.

It may be easier for the user to see what's been selected instead of scrolling a
long listbox????



Thanks this is a much better idea, have implemented this in my
workbook

cheers

Paul
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Two Visible Ranges (From Two Sheets) To A New Workbook (AlsoWith Two Sheets) [email protected] Excel Programming 2 January 29th 09 08:39 PM
Copy Two Visible Ranges (From Two Sheets) To A New Workbook (AlsoWith Two Sheets) [email protected] Excel Programming 0 January 29th 09 01:35 PM
VBA to Select All Visible Sheets bill_morgan Excel Programming 4 March 1st 06 05:57 AM
Select Visible sheets Darin Kramer Excel Programming 5 January 31st 05 03:48 PM
listbox shrinks to a line when changing between sheets if I use the visible property bikerchick Excel Programming 0 July 7th 04 04:07 PM


All times are GMT +1. The time now is 03:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"