View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Sam Kuo[_3_] Sam Kuo[_3_] is offline
external usenet poster
 
Posts: 86
Default Redim Preserve question

Thanks for your reply.
Yes, I've tried to assign the subroutine to 100 textboxes, named in the
format of txtCatchZPreYX (e.g. txtCatch1PreY1, ..., txtCatch1PreY10,
....txtCatch10PreY1, ..., txtCatch10PreY10). And would like to be able call
out the Z value associated with each textbox, if it's doable?



"Tim Zych" wrote:

What's Z supposed to do? Your addition would make X loop 10 times for each
Z, or 100 times total and create multiple instances of the control event
reference.


--
Tim Zych
www.higherdata.com
Compare data in workbooks and find differences with Workbook Compare
A free, powerful, flexible Excel utility

"Sam Kuo" wrote in message
...
Hi Tim, thanks for your swift reply.

I've just added another Z loop outside the X loop to cater for more
textboxes
(i.e. txtCatchZPreYX, where Z is a number of 1 to 10, and X is also a
number
of 1 to 10) and it still works great.

Is there a way to "preserve" the Z value for each textbox, so that I can
call it out in the textbox_change subroutine?

Below is what I've tried, but obviously the Z value is not stored and
returns as nothing when execute the subroutine...

Sub ShowDialog()
Dim ctrl As Control
Dim TextboxCount As Integer, X As Integer
TextboxCount = 0
' I tried adding another Z loop
For Z = 1 To 10
For X = 1 To 10
On Error Resume Next
Set ctrl = Nothing
Set ctrl = UserForm1.Controls("txtCatch1PreY" & X)
On Error GoTo 0
If Not ctrl Is Nothing Then
TextboxCount = TextboxCount + 1
ReDim Preserve TextBoxes(1 To TextboxCount)
Set TextBoxes(TextboxCount).SlopeTextboxGroup = ctrl
End If
Next X
Next Z
UserForm1.Show
End Sub

Private Sub SlopeTextboxGroup_Change()
MsgBox "Hello from " & SlopeTextboxGroup.Name
' But Z value is not stored....
MsgBox Z
End Sub



"Tim Zych" wrote:

When I ran your macro, the control and the counter got "out of synch",
and
only a couple were being set.

Using your specs of 10 textboxes named Y1...Y10, this is another way to
do
it.

Sub ShowDialog()
Dim ctrl As Control
Dim TextboxCount As Integer, X As Integer
TextboxCount = 0
For X = 1 To 10
On Error Resume Next
Set ctrl = Nothing
Set ctrl = UserForm1.Controls("txtCatch1PreY" & X)
On Error GoTo 0
If Not ctrl Is Nothing Then
TextboxCount = TextboxCount + 1
ReDim Preserve TextBoxes(1 To TextboxCount)
Set TextBoxes(TextboxCount).SlopeTextboxGroup = ctrl
End If
Next
UserForm1.Show
End Sub

--
Tim Zych
www.higherdata.com
Compare data in workbooks and find differences with Workbook Compare
A free, powerful, flexible Excel utility

"Sam Kuo" wrote in message
...
I tried to modify John Walkenbach's example in the link below (i.e.
assigning
one subroutine to multiple buttons) to do the same task but for
textboxes
instead of buttons:
http://www.j-walk.com/ss/excel/tips/tip44.htm

e.g. I have several textboxes, and I only want to assign a subroutine
to
10
particular textboxes (namely txtCatch1PreY1 to txtCatch1PreY10). But my
attempt below seems to assign the subroutine to txtCatch1PreY1 only.
Can
someone please help me spot where the problem is in my code?

' The following code is in Class Module 1
Public WithEvents SlopeTextboxGroup As MSForms.TextBox

Private Sub SlopeTextboxGroup_Change()
MsgBox "Hello from " & SlopeTextboxGroup.Name
End Sub


' The following code is in Module 1
Option Explicit

Dim TextBoxes() As New Class1

Sub ShowDialog()
Dim ctrl As Control
Dim TextboxCount As Integer

TextboxCount = 1
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Name = "txtCatch1PreY" & TextboxCount Then
TextboxCount = TextboxCount + 1
ReDim Preserve TextBoxes(1 To TextboxCount)
Set TextBoxes(TextboxCount).SlopeTextboxGroup = ctrl
End If
End If
Next ctrl
' Show UserForm1
UserForm1.Show
End Sub