ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Listing Userform Names (https://www.excelbanter.com/excel-programming/374881-listing-userform-names.html)

Nigel RS[_2_]

Listing Userform Names
 
Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport





Alok

Listing Userform Names
 
I know you are looking for an explanation to your question. I do not have
that. However, you could iterate through the forms like this(if they are
already loaded)

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i


"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport





Alok

Listing Userform Names
 
While I do not have an answer to your good question as I too have wondered
about that.. here is another way of iterating through the loaded collection
of forms

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i


"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport





Bob Phillips

Listing Userform Names
 
That is because a user form does not exist in the userforms collection until
after it has been loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Nigel RS" wrote in message
...
Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport







Bob Phillips

Listing Userform Names
 
This will return nothing in most instances, unless the userforms have been
loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Alok" wrote in message
...
I know you are looking for an explanation to your question. I do not have
that. However, you could iterate through the forms like this(if they are
already loaded)

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i


"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the

second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport







Alok

Listing Userform Names
 
Sorry for the similar two responses already to your question. The first time
I got an error and thought the response did not go through.

Here is anothe way that I found one can iterate

Dim uf As Object
For Each uf In UserForms
Debug.Print uf.Caption
Next uf

This is taken from Chip Pearson's explanation.
http://groups.google.com/group/micro...3e7c01 a6ff7f

"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport





Alok

Listing Userform Names
 
Bob,
I did say that.?

"Bob Phillips" wrote:

This will return nothing in most instances, unless the userforms have been
loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Alok" wrote in message
...
I know you are looking for an explanation to your question. I do not have
that. However, you could iterate through the forms like this(if they are
already loaded)

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i


"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the

second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport








Peter T

Listing Userform Names
 
Sub test()
Dim oVBP As Object ' VBProject
Dim oCmp As Object ' VBComponent
Dim ctl As Control

For Each oCmp In ThisWorkbook.VBProject.VBComponents
If oCmp.Type = 3& Then ' userform module
Debug.Print oCmp.Name
For Each ctl In oCmp.designer.Controls
Debug.Print , ctl.Name
Next
End If
Next

End Sub

You would need Trust access to VBProjects but not necessary to load the
forms

Regards,
Peter T

"Nigel RS" wrote in message
...
Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport







Nigel

Listing Userform Names
 
I see. Nut do not understand why?

If I define uf as a userform my code fails, if I define it as an object and
then declare it in the loop by assigning each userform it does!!

Not very intuitive, but then you live and learn

Many thanks for the feedback

--
Cheers
Nigel



"Alok" wrote in message
...
Sorry for the similar two responses already to your question. The first
time
I got an error and thought the response did not go through.

Here is anothe way that I found one can iterate

Dim uf As Object
For Each uf In UserForms
Debug.Print uf.Caption
Next uf

This is taken from Chip Pearson's explanation.
http://groups.google.com/group/micro...3e7c01 a6ff7f

"Nigel RS" wrote:

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the
second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport







Nigel

Listing Userform Names
 
Neat way of avoiding the loading issue. thanks

--
Cheers
Nigel



"Peter T" <peter_t@discussions wrote in message
...
Sub test()
Dim oVBP As Object ' VBProject
Dim oCmp As Object ' VBComponent
Dim ctl As Control

For Each oCmp In ThisWorkbook.VBProject.VBComponents
If oCmp.Type = 3& Then ' userform module
Debug.Print oCmp.Name
For Each ctl In oCmp.designer.Controls
Debug.Print , ctl.Name
Next
End If
Next

End Sub

You would need Trust access to VBProjects but not necessary to load the
forms

Regards,
Peter T

"Nigel RS" wrote in message
...
Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the
second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport










All times are GMT +1. The time now is 10:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com