PC Review


Reply
Thread Tools Rate Thread

Common Function for getting userform information

 
 
Danny
Guest
Posts: n/a
 
      24th Dec 2009
Hi,

My macro has a lot of userform and multi listbox within userform (each
userform has unique name). how can I use a function to get the
selected items in different listbox?

eg.
fmMyPlot.lb_Prof_xAxis
fmMyPlot.lb_Prof_y1Axis

fmMyPlot.lb_Std_Gp
fmMyPlot.lb_Std_Series

fmMyTemplate.lb_temp1
fmMyTemplate.lb_temp2

any ideas? thx
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      24th Dec 2009
Sub Test()
Dim i As Long, n As Long
Dim s As String
Dim arr
Dim lb As MSForms.ListBox

Set lb = UserForm1.ListBox1

n = getSelected(lb, arr)

If n = -1 Then
s = "no items selected"
Else
s = arr(0)
For i = 1 To UBound(arr)
s = s & vbCr & arr(i)
Next
End If

MsgBox s

End Sub

Function getSelected(lb As MSForms.ListBox, arr) As Long
Dim i As Long, idx As Long

idx = -1

ReDim arr(0 To lb.ListCount - 1)
For i = 0 To lb.ListCount - 1
If lb.Selected(i) Then
idx = idx + 1
arr(idx) = lb.List(i)
End If
Next
If idx >= 0 And idx < UBound(arr) Then
ReDim Preserve arr(0 To idx)
End If
getSelected = idx

End Function

Regards,
Peter T

"Danny" <(E-Mail Removed)> wrote in message
news:98f44296-fe48-4652-a999-(E-Mail Removed)...
> Hi,
>
> My macro has a lot of userform and multi listbox within userform (each
> userform has unique name). how can I use a function to get the
> selected items in different listbox?
>
> eg.
> fmMyPlot.lb_Prof_xAxis
> fmMyPlot.lb_Prof_y1Axis
>
> fmMyPlot.lb_Std_Gp
> fmMyPlot.lb_Std_Series
>
> fmMyTemplate.lb_temp1
> fmMyTemplate.lb_temp2
>
> any ideas? thx



 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      24th Dec 2009
On Dec 24, 6:26*pm, "Peter T" <peter_t@discussions> wrote:
> Sub Test()
> Dim i As Long, n As Long
> Dim s As String
> Dim arr
> Dim lb As MSForms.ListBox
>
> * * Set lb = UserForm1.ListBox1
>
> * * n = getSelected(lb, arr)
>
> * * If n = -1 Then
> * * * * s = "no items selected"
> * * Else
> * * * * s = arr(0)
> * * * * For i = 1 To UBound(arr)
> * * * * * * s = s & vbCr & arr(i)
> * * * * Next
> * * End If
>
> * * MsgBox s
>
> End Sub
>
> Function getSelected(lb As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
>
> * * idx = -1
>
> * * ReDim arr(0 To lb.ListCount - 1)
> * * For i = 0 To lb.ListCount - 1
> * * * * If lb.Selected(i) Then
> * * * * * * idx = idx + 1
> * * * * * * arr(idx) = lb.List(i)
> * * * * End If
> * * Next
> * * If idx >= 0 And idx < UBound(arr) Then
> * * * * ReDim Preserve arr(0 To idx)
> * * End If
> * * getSelected = idx
>
> End Function
>
> Regards,
> Peter T
>
> "Danny" <dannypct...@gmail.com> wrote in message
>
> news:98f44296-fe48-4652-a999-(E-Mail Removed)...
>
>
>
> > Hi,

>
> > My macro has a lot of userform and multi listbox within userform (each
> > userform has unique name). how can I use a function to get the
> > selected items in different listbox?

>
> > eg.
> > fmMyPlot.lb_Prof_xAxis
> > fmMyPlot.lb_Prof_y1Axis

>
> > fmMyPlot.lb_Std_Gp
> > fmMyPlot.lb_Std_Series

>
> > fmMyTemplate.lb_temp1
> > fmMyTemplate.lb_temp2

>
> > any ideas? thx- Hide quoted text -

>
> - Show quoted text -



Hi, Thanks.

i tried to modify the second part, but failed.
mainly, the array changed from Array(x) to Array(x,2)

++++++++++++++++++
Option Base 1
Function getSelected(lb As MSForms.ListBox, arr) As Long
Dim i As Long, idx As Long

idx = 0
ReDim arr(1 To lb.ListCount, 2)
For i = 1 To lb.ListCount
If lb.Selected(i - 1) = True Then
idx = idx + 1
arr(idx, 1) = lb.List(i - 1)
arr(idx, 2) = i
End If
Next
If idx >= 1 And idx < UBound(arr) Then
ReDim Preserve arr(1 To idx)
End If
getSelected = idx

End Function
++++++++++++++++++
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      24th Dec 2009
From the Remarks section of the help file for ReDim...

"If you use the Preserve keyword, you can resize
only the last array dimension and you can't change
the number of dimensions at all."

--
Rick (MVP - Excel)


"Danny" <(E-Mail Removed)> wrote in message
news:685853e7-a1a8-4d20-94ac-(E-Mail Removed)...
On Dec 24, 6:26 pm, "Peter T" <peter_t@discussions> wrote:
> Sub Test()
> Dim i As Long, n As Long
> Dim s As String
> Dim arr
> Dim lb As MSForms.ListBox
>
> Set lb = UserForm1.ListBox1
>
> n = getSelected(lb, arr)
>
> If n = -1 Then
> s = "no items selected"
> Else
> s = arr(0)
> For i = 1 To UBound(arr)
> s = s & vbCr & arr(i)
> Next
> End If
>
> MsgBox s
>
> End Sub
>
> Function getSelected(lb As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
>
> idx = -1
>
> ReDim arr(0 To lb.ListCount - 1)
> For i = 0 To lb.ListCount - 1
> If lb.Selected(i) Then
> idx = idx + 1
> arr(idx) = lb.List(i)
> End If
> Next
> If idx >= 0 And idx < UBound(arr) Then
> ReDim Preserve arr(0 To idx)
> End If
> getSelected = idx
>
> End Function
>
> Regards,
> Peter T
>
> "Danny" <dannypct...@gmail.com> wrote in message
>
> news:98f44296-fe48-4652-a999-(E-Mail Removed)...
>
>
>
> > Hi,

>
> > My macro has a lot of userform and multi listbox within userform (each
> > userform has unique name). how can I use a function to get the
> > selected items in different listbox?

>
> > eg.
> > fmMyPlot.lb_Prof_xAxis
> > fmMyPlot.lb_Prof_y1Axis

>
> > fmMyPlot.lb_Std_Gp
> > fmMyPlot.lb_Std_Series

>
> > fmMyTemplate.lb_temp1
> > fmMyTemplate.lb_temp2

>
> > any ideas? thx- Hide quoted text -

>
> - Show quoted text -



Hi, Thanks.

i tried to modify the second part, but failed.
mainly, the array changed from Array(x) to Array(x,2)

++++++++++++++++++
Option Base 1
Function getSelected(lb As MSForms.ListBox, arr) As Long
Dim i As Long, idx As Long

idx = 0
ReDim arr(1 To lb.ListCount, 2)
For i = 1 To lb.ListCount
If lb.Selected(i - 1) = True Then
idx = idx + 1
arr(idx, 1) = lb.List(i - 1)
arr(idx, 2) = i
End If
Next
If idx >= 1 And idx < UBound(arr) Then
ReDim Preserve arr(1 To idx)
End If
getSelected = idx

End Function
++++++++++++++++++

 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      25th Dec 2009
On Dec 25, 3:53*am, "Rick Rothstein"
<rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> From the Remarks section of the help file for ReDim...
>
> "If you use the Preserve keyword, you can resize
> *only the last array dimension and you can't change
> *the number of dimensions at all."
>
> --
> Rick (MVP - Excel)
>
> "Danny" <dannypct...@gmail.com> wrote in message
>
> news:685853e7-a1a8-4d20-94ac-(E-Mail Removed)...
> On Dec 24, 6:26 pm, "Peter T" <peter_t@discussions> wrote:
>
>
>
>
>
> > Sub Test()
> > Dim i As Long, n As Long
> > Dim s As String
> > Dim arr
> > Dim lb As MSForms.ListBox

>
> > Set lb = UserForm1.ListBox1

>
> > n = getSelected(lb, arr)

>
> > If n = -1 Then
> > s = "no items selected"
> > Else
> > s = arr(0)
> > For i = 1 To UBound(arr)
> > s = s & vbCr & arr(i)
> > Next
> > End If

>
> > MsgBox s

>
> > End Sub

>
> > Function getSelected(lb As MSForms.ListBox, arr) As Long
> > Dim i As Long, idx As Long

>
> > idx = -1

>
> > ReDim arr(0 To lb.ListCount - 1)
> > For i = 0 To lb.ListCount - 1
> > If lb.Selected(i) Then
> > idx = idx + 1
> > arr(idx) = lb.List(i)
> > End If
> > Next
> > If idx >= 0 And idx < UBound(arr) Then
> > ReDim Preserve arr(0 To idx)
> > End If
> > getSelected = idx

>
> > End Function

>
> > Regards,
> > Peter T

>
> > "Danny" <dannypct...@gmail.com> wrote in message

>
> >news:98f44296-fe48-4652-a999-(E-Mail Removed)....

>
> > > Hi,

>
> > > My macro has a lot of userform and multi listbox within userform (each
> > > userform has unique name). how can I use a function to get the
> > > selected items in different listbox?

>
> > > eg.
> > > fmMyPlot.lb_Prof_xAxis
> > > fmMyPlot.lb_Prof_y1Axis

>
> > > fmMyPlot.lb_Std_Gp
> > > fmMyPlot.lb_Std_Series

>
> > > fmMyTemplate.lb_temp1
> > > fmMyTemplate.lb_temp2

>
> > > any ideas? thx- Hide quoted text -

>
> > - Show quoted text -

>
> Hi, Thanks.
>
> i tried to modify the second part, but failed.
> mainly, the array changed from Array(x) to Array(x,2)
>
> ++++++++++++++++++
> Option Base 1
> Function getSelected(lb As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
>
> * * idx = 0
> * * ReDim arr(1 To lb.ListCount, 2)
> * * For i = 1 To lb.ListCount
> * * * * If lb.Selected(i - 1) = True Then
> * * * * * * idx = idx + 1
> * * * * * * arr(idx, 1) = lb.List(i - 1)
> * * * * * * arr(idx, 2) = i
> * * * * End If
> * * Next
> * * If idx >= 1 And idx < UBound(arr) Then
> * * * * ReDim Preserve arr(1 To idx)
> * * End If
> * * getSelected = idx
>
> End Function
> ++++++++++++++++++- Hide quoted text -
>
> - Show quoted text -


Hi, Thanks.

then i will separate this set array to 2.

br,
Danny
 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      26th Dec 2009

"Danny" <(E-Mail Removed)> wrote in message
news:685853e7-a1a8-4d20-94ac-(E-Mail Removed)...
On Dec 24, 6:26 pm, "Peter T" <peter_t@discussions> wrote:
> Sub Test()
> Dim i As Long, n As Long
> Dim s As String
> Dim arr
> Dim lb As MSForms.ListBox
>
> Set lb = UserForm1.ListBox1
>
> n = getSelected(lb, arr)
>
> If n = -1 Then
> s = "no items selected"
> Else
> s = arr(0)
> For i = 1 To UBound(arr)
> s = s & vbCr & arr(i)
> Next
> End If
>
> MsgBox s
>
> End Sub
>
> Function getSelected(lb As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
>
> idx = -1
>
> ReDim arr(0 To lb.ListCount - 1)
> For i = 0 To lb.ListCount - 1
> If lb.Selected(i) Then
> idx = idx + 1
> arr(idx) = lb.List(i)
> End If
> Next
> If idx >= 0 And idx < UBound(arr) Then
> ReDim Preserve arr(0 To idx)
> End If
> getSelected = idx
>
> End Function
>
> Regards,
> Peter T
>
> "Danny" <dannypct...@gmail.com> wrote in message
>
> news:98f44296-fe48-4652-a999-(E-Mail Removed)...
>
>
>
> > Hi,

>
> > My macro has a lot of userform and multi listbox within userform (each
> > userform has unique name). how can I use a function to get the
> > selected items in different listbox?

>
> > eg.
> > fmMyPlot.lb_Prof_xAxis
> > fmMyPlot.lb_Prof_y1Axis

>
> > fmMyPlot.lb_Std_Gp
> > fmMyPlot.lb_Std_Series

>
> > fmMyTemplate.lb_temp1
> > fmMyTemplate.lb_temp2

>
> > any ideas? thx- Hide quoted text -

>
> - Show quoted text -



Hi, Thanks.

i tried to modify the second part, but failed.
mainly, the array changed from Array(x) to Array(x,2)

++++++++++++++++++
Option Base 1
Function getSelected(lb As MSForms.ListBox, arr) As Long
Dim i As Long, idx As Long

idx = 0
ReDim arr(1 To lb.ListCount, 2)
For i = 1 To lb.ListCount
If lb.Selected(i - 1) = True Then
idx = idx + 1
arr(idx, 1) = lb.List(i - 1)
arr(idx, 2) = i
End If
Next
If idx >= 1 And idx < UBound(arr) Then
ReDim Preserve arr(1 To idx)
End If
getSelected = idx

End Function
++++++++++++++++++

========================================================


As Rick says you can only Redim Preserve the last dimension. Here's same
again adapted to a get selected items from a two column multiselect Listbox

Sub Test()
Dim i As Long, n As Long
Dim s As String
Dim arr
Dim lbx As MSForms.ListBox

Set lbx = UserForm1.ListBox1 ' assumes 2-columns

n = getSelected(lbx, arr)

If n = -1 Then
s = "no items selected"
Else
s = arr(0, 0) & vbTab & arr(1, 0)
For i = 1 To UBound(arr, 2)
s = s & vbCr & arr(0, i) & vbTab & arr(1, i)
Next
End If

MsgBox s

End Sub

Function getSelected(lbx As MSForms.ListBox, arr) As Long
Dim i As Long, idx As Long
' to get an array of seleted items from
' a Multiselect, 2-column ListBox

idx = -1

ReDim arr(0 To 1, 0 To lbx.ListCount - 1)
For i = 0 To lbx.ListCount - 1
If lbx.Selected(i) Then
idx = idx + 1
arr(0, idx) = lbx.List(i, 0)
arr(1, idx) = lbx.List(i, 1)
End If
Next
If idx >= 0 And idx < UBound(arr) Then
ReDim Preserve arr(0 To 1, 0 To idx)
End If
getSelected = idx

End Function


This is all zero-base, adapt if you particularly want to change to one-base.

Regards,
Peter T


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      26th Dec 2009

"Peter T" <peter_t@discussions> wrote in message
news:uRv%(E-Mail Removed)...
>
> "Danny" <(E-Mail Removed)> wrote in message
> news:685853e7-a1a8-4d20-94ac-(E-Mail Removed)...
> On Dec 24, 6:26 pm, "Peter T" <peter_t@discussions> wrote:
>> Sub Test()
>> Dim i As Long, n As Long
>> Dim s As String
>> Dim arr
>> Dim lb As MSForms.ListBox
>>
>> Set lb = UserForm1.ListBox1
>>
>> n = getSelected(lb, arr)
>>
>> If n = -1 Then
>> s = "no items selected"
>> Else
>> s = arr(0)
>> For i = 1 To UBound(arr)
>> s = s & vbCr & arr(i)
>> Next
>> End If
>>
>> MsgBox s
>>
>> End Sub
>>
>> Function getSelected(lb As MSForms.ListBox, arr) As Long
>> Dim i As Long, idx As Long
>>
>> idx = -1
>>
>> ReDim arr(0 To lb.ListCount - 1)
>> For i = 0 To lb.ListCount - 1
>> If lb.Selected(i) Then
>> idx = idx + 1
>> arr(idx) = lb.List(i)
>> End If
>> Next
>> If idx >= 0 And idx < UBound(arr) Then
>> ReDim Preserve arr(0 To idx)
>> End If
>> getSelected = idx
>>
>> End Function
>>
>> Regards,
>> Peter T
>>
>> "Danny" <dannypct...@gmail.com> wrote in message
>>
>> news:98f44296-fe48-4652-a999-(E-Mail Removed)...
>>
>>
>>
>> > Hi,

>>
>> > My macro has a lot of userform and multi listbox within userform (each
>> > userform has unique name). how can I use a function to get the
>> > selected items in different listbox?

>>
>> > eg.
>> > fmMyPlot.lb_Prof_xAxis
>> > fmMyPlot.lb_Prof_y1Axis

>>
>> > fmMyPlot.lb_Std_Gp
>> > fmMyPlot.lb_Std_Series

>>
>> > fmMyTemplate.lb_temp1
>> > fmMyTemplate.lb_temp2

>>
>> > any ideas? thx- Hide quoted text -

>>
>> - Show quoted text -

>
>
> Hi, Thanks.
>
> i tried to modify the second part, but failed.
> mainly, the array changed from Array(x) to Array(x,2)
>
> ++++++++++++++++++
> Option Base 1
> Function getSelected(lb As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
>
> idx = 0
> ReDim arr(1 To lb.ListCount, 2)
> For i = 1 To lb.ListCount
> If lb.Selected(i - 1) = True Then
> idx = idx + 1
> arr(idx, 1) = lb.List(i - 1)
> arr(idx, 2) = i
> End If
> Next
> If idx >= 1 And idx < UBound(arr) Then
> ReDim Preserve arr(1 To idx)
> End If
> getSelected = idx
>
> End Function
> ++++++++++++++++++
>
> ========================================================
>
>
> As Rick says you can only Redim Preserve the last dimension. Here's same
> again adapted to a get selected items from a two column multiselect
> Listbox
>
> Sub Test()
> Dim i As Long, n As Long
> Dim s As String
> Dim arr
> Dim lbx As MSForms.ListBox
>
> Set lbx = UserForm1.ListBox1 ' assumes 2-columns
>
> n = getSelected(lbx, arr)
>
> If n = -1 Then
> s = "no items selected"
> Else
> s = arr(0, 0) & vbTab & arr(1, 0)
> For i = 1 To UBound(arr, 2)
> s = s & vbCr & arr(0, i) & vbTab & arr(1, i)
> Next
> End If
>
> MsgBox s
>
> End Sub
>
> Function getSelected(lbx As MSForms.ListBox, arr) As Long
> Dim i As Long, idx As Long
> ' to get an array of seleted items from
> ' a Multiselect, 2-column ListBox
>
> idx = -1
>
> ReDim arr(0 To 1, 0 To lbx.ListCount - 1)
> For i = 0 To lbx.ListCount - 1
> If lbx.Selected(i) Then
> idx = idx + 1
> arr(0, idx) = lbx.List(i, 0)
> arr(1, idx) = lbx.List(i, 1)
> End If
> Next
> If idx >= 0 And idx < UBound(arr) Then
> ReDim Preserve arr(0 To 1, 0 To idx)
> End If
> getSelected = idx
>
> End Function
>
>
> This is all zero-base, adapt if you particularly want to change to
> one-base.
>
> Regards,
> Peter T

==========================================================

Looks like I misunderstood the purpose of your 2d array, I assumed it was
because you have 2-column Listboxes. But reading again I see you have this -

arr(idx, 1) = lb.List(i - 1)
arr(idx, 2) = i

I don't know what you are attempting to do and suspect my revised demo is
not what you want.

Regards,
Peter T


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating fields together with common information Daniel Pineault Microsoft Access Queries 6 26th Nov 2007 01:29 PM
How do I have common information populate each template in BCM? =?Utf-8?B?UGF6?= Microsoft Outlook BCM 0 16th Oct 2007 09:44 PM
Create column of common items from information in 4 columns Steve Microsoft Excel Discussion 2 25th Apr 2006 02:41 PM
Common format for Labels in Userform edgargracias@hotmail.com Microsoft Excel Programming 3 13th May 2004 11:14 AM
Update several Contact with Common information =?Utf-8?B?RnJhbWUgQm95?= Microsoft Outlook Contacts 1 11th Jan 2004 06:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:45 PM.