Word UserForm problem driving me batty

D

DeanL

Hi everyone,

I've been trying to solve what is probably an extremely simple problem
and desperately need some assistance.

My problem is that I have a Word UserForm with 5 textboxes that all
have an initial string set to 0, now what I need is when someone
changes the values in the textboxes and clicks on the OK button that
the values in the textboxes will be formatted to the format 0000
(already got this done), and a string created from the textboxes with
appropriate separators (either a comma or ampersand).

The process would work fine if all five textboxes are always filled
out (i.e. 0001, 0002, 0003, 0004 & 0005) but I need a way to work with
any number of textboxes from 1 to 5 and omit the textboxes that have
not been changed from 0 as shown below:

1. 0001
2. 0001 & 0002
3. 0001, 0002 & 0003
4. 0001, 0002, 0003 & 0004
5. 0001, 0002, 0003, 0004 & 0005

I hope someone can point me in the right direction with this as I
currently don't have much more hair left to pull out.

Many thanks, Dean...
 
D

Doug Robbins - Word MVP

Assuming that the second text box is not filled unless the first is filled,
etc., use:

Dim result As String
If Text1.Text <> 0 Then
result = Format(Text1.Text, "0000")
End If
If Text2.Text <> 0 Then
result = result & " & " & Format(Text2.Text, "0000")
End If
If Text3.Text <> 0 Then
result = Replace(result, " & ", ", ") & " & " & Format(Text3.Text,
"0000")
End If
If Text4.Text <> 0 Then
result = Replace(result, " & ", ", ") & " & " & Format(Text4.Text,
"0000")
End If
If Text5.Text <> 0 Then
result = Replace(result, " & ", ", ") & " & " & Format(Text5.Text,
"0000")
End If

If the text boxes are not filled in sequence, that is, there are gaps, it
becomes significantly more complex.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

DeanL

Hi Doug,

Sorry for the delay in replying. This code snippet works like a charm
and does deal with the textboxes correctly even if they are NOT in
sequence as long as the first textbox has a value then the result is
shown correctly.

So thank you so much and you rock.

All the best, Dean...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top