PC Review


Reply
Thread Tools Rate Thread

array of labels

 
 
Wiley Post
Guest
Posts: n/a
 
      19th Apr 2008
I want to modify a UserForm dynamically by adding some number of labels from
my VB/Excel program:
Dim labels() as MSForms.Label
:
ReDim labels(NumLabels)

At this point I have an array of Labels with each value set to Nothing. If
I do this:

Set labels(0) = New MSForms.Label
I get runtime "Invalid use of New keyword" and Help says I cannot reference
a class name in this context -- I need to reference a specific object name,
e.g. Label1. So I tried:

Dim Label1 as MSForms.Label
Set labels(0) = New Label1

and I get "User-defined type not defined"

I have tried other things such as
Dim labels() as New MSForms.Label
and I also get errors.

How do I programmatically create an array of Labels so that I can place them
on my UserForm?


 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      19th Apr 2008
Hi

I don't think you can do what you are trying to.

I would put the required number of labels on my userform and leave the
caption blank, then use VBA populate the labels needed.

Regards,
Per

"Wiley Post" <(E-Mail Removed)> skrev i meddelelsen
news:(E-Mail Removed)...
>I want to modify a UserForm dynamically by adding some number of labels
>from my VB/Excel program:
> Dim labels() as MSForms.Label
> :
> ReDim labels(NumLabels)
>
> At this point I have an array of Labels with each value set to Nothing.
> If I do this:
>
> Set labels(0) = New MSForms.Label
> I get runtime "Invalid use of New keyword" and Help says I cannot
> reference a class name in this context -- I need to reference a specific
> object name, e.g. Label1. So I tried:
>
> Dim Label1 as MSForms.Label
> Set labels(0) = New Label1
>
> and I get "User-defined type not defined"
>
> I have tried other things such as
> Dim labels() as New MSForms.Label
> and I also get errors.
>
> How do I programmatically create an array of Labels so that I can place
> them on my UserForm?
>


 
Reply With Quote
 
Wiley Post
Guest
Posts: n/a
 
      19th Apr 2008
I was trying to make one UserForm that could be modified at run time for a
few different callers. I'm a bit surprised that it can't be done, or at
least the modifications I want can't be done.

WP

"Per Jessen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I don't think you can do what you are trying to.
>
> I would put the required number of labels on my userform and leave the
> caption blank, then use VBA populate the labels needed.
>
> Regards,
> Per
>
> "Wiley Post" <(E-Mail Removed)> skrev i meddelelsen
> news:(E-Mail Removed)...
>>I want to modify a UserForm dynamically by adding some number of labels
>>from my VB/Excel program:
>> Dim labels() as MSForms.Label
>> :
>> ReDim labels(NumLabels)
>>
>> At this point I have an array of Labels with each value set to Nothing.
>> If I do this:
>>
>> Set labels(0) = New MSForms.Label
>> I get runtime "Invalid use of New keyword" and Help says I cannot
>> reference a class name in this context -- I need to reference a specific
>> object name, e.g. Label1. So I tried:
>>
>> Dim Label1 as MSForms.Label
>> Set labels(0) = New Label1
>>
>> and I get "User-defined type not defined"
>>
>> I have tried other things such as
>> Dim labels() as New MSForms.Label
>> and I also get errors.
>>
>> How do I programmatically create an array of Labels so that I can place
>> them on my UserForm?
>>

>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2008
You can add controls to a userform during runtime, but I've always found it
much, much, much easier to just add as many controls as I'll ever need--but hide
the ones that I don't need right away.

Then I can unhide the ones I want when I need them.

It makes life a lot simpler this way.

John Walkenbach shows how to create a userform on the fly. This includes adding
controls to that userform:
http://j-walk.com/ss/excel/tips/tip76.htm

This worked ok for me:

Option Explicit
Private Sub CommandButton1_Click()
Dim iCtr As Long

For iCtr = 1 To 3
With Controls.Add(bstrprogid:="Forms.Label.1", _
Name:="LBL_" & iCtr, Visible:=True)
.Left = 5
.Top = iCtr * 15
.Height = 10
.Width = 200
.Caption = "Hi from " & .Name
End With
Next iCtr
End Sub

Wiley Post wrote:
>
> I want to modify a UserForm dynamically by adding some number of labels from
> my VB/Excel program:
> Dim labels() as MSForms.Label
> :
> ReDim labels(NumLabels)
>
> At this point I have an array of Labels with each value set to Nothing. If
> I do this:
>
> Set labels(0) = New MSForms.Label
> I get runtime "Invalid use of New keyword" and Help says I cannot reference
> a class name in this context -- I need to reference a specific object name,
> e.g. Label1. So I tried:
>
> Dim Label1 as MSForms.Label
> Set labels(0) = New Label1
>
> and I get "User-defined type not defined"
>
> I have tried other things such as
> Dim labels() as New MSForms.Label
> and I also get errors.
>
> How do I programmatically create an array of Labels so that I can place them
> on my UserForm?


--

Dave Peterson
 
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
get all the labels of all forms in array RB Smissaert Microsoft Excel Programming 3 23rd Oct 2005 08:13 PM
how to create a array of labels in vb.net? Soon Lee Microsoft Dot NET 2 3rd Jan 2005 10:25 AM
Array of labels?? jaxrpc Microsoft Excel Misc 2 3rd Jan 2004 04:03 PM
Populating labels from an array Ian Microsoft VB .NET 2 27th Nov 2003 08:17 AM
Array of Labels Jan Pit Microsoft Access Reports 2 2nd Nov 2003 09:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.