Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I increment numbers? 1

Status
Not open for further replies.

MarcusStringer

IS-IT--Management
Sep 11, 2003
1,407
AU
If i have a text field with the number 1, How can I make it add 1 then display the result? eg
This number is 1
This number is 2
This number is 3 etc etc.
 
1. "Textbox"...lordy I hate that word. Sigh, what kind of a textbox? Is this textbox on a UserForm, is it in the document? If it is in the document, is it a FormField textbox? Is it an ActiveX textbox? Is it one of those horrible Insert > Textbox Shapes textboxes?

2. Is this a textbox that is asking for user input? If it is NOT, and you are just using it to DISPLAY text (number...whatever), please reconsider NOT using a textbox. If this is a textbox on a UserForm, definitely reconsider using a Label instead of a textbox. Textboxes should be used for input, not display.

It depends on, again what kind of textbox it is, and where the original number is coming from. It is coming from a variable, as in:

var = 1
Textbox1.TeXt = "This is number " & var

In which case, it would simply be:
Textbox1.Text = "This is number " & (var + 1)

Is is coming into the (whatever kind of textbox) as a string?
ActiveDocument.Formfields("Text1").Result = "This is number 1"

If you give better details, there is a simple solution.

If a variable - then variable + 1
If a string - then change the string. Say the number is coming in from somewhere else, you could strip off that last string ( the "1"), and add to it. Something like:
Code:
Sub StringAddOne()
Dim sText As String
sText = "This is number 1"
MsgBox Left(sText, (Len(sText) - 1)) & (Right(sText, 1) + 1)
End Sub

The message box will display "This is number 2".

Or you put that as a function - as long as it is always going to be the last letter, AND it is not going to increment to two characters - no more than 9. Otherwise you have to adjust this function.

Code:
Function StringAddOne (sText as String)
StringAddOne = Left(sText, (Len(sText) - 1)) & (Right(sText, 1) + 1)
End Function

Sub
Textbox1.Text = StringAddOne(Textbox1.Text)
End Sub
Or if a Form Field:

Code:
ActiveDocument.Formfields("Text1").Result = _
    StringAddOne(ActiveDocument.Formfields("Text1").Result)

I am sure there is a smuch simpler way, if you gave more details of where, and how the "textbox" is getting its value.

Gerry
 
Sorry,
I have a document.
I made a macro which does other stuff as well, and it works, but I also need to put the word "End "
at the end of each paragraph, which I've done. What I can't work out is how I can put the number in eg.
hjkfldshfjdjsjkhdksjhfjkdhkfjhjkhdfhjlhjklhfdsjahjdshjdsahjkfdshjkfldshjkfldhsjkf ."End 1"

hfdjkslahfjlfdsafdsfdsfdsafdsafdsafdafdfdsafdafdsafdsafdsafdsafdsafdsa. "End 2"

hfdjkslahfjlfdsafdsfdsfdsafdsafdsafdafdfdsafdafdsafdsafdsafdsafdsafdsa."End 3" etc.


Marcus
 
Why not posting your macro code, explaining us where you're stuck ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes, because again, that is not enough detail. Those paragraphs - are you getting them from somewhere else, and copying into the document? Are you generating the text? Are those paragraphs coming in one at a time, in a bunch? What? Is this part of a selection?

I mean, it sure is not clear.

Code:
Sub PutEndText()
Dim i As Integer
Dim sEndText As String
Dim mPara As Paragraph
i = 1
For Each mPara In ActiveDocument.Paragraphs
  Selection.Start = mPara.Range.End - 1
  sEndText = "  End " & i
  Selection.TypeText Text:=sEndText
  i = i + 1
Next
End Sub

puts "End" and a incrementing number after every paragraph in the document. Is this what you want?

Gerry
 
Thanks for all of your help Gerry.

Your example worked a treat.

BTW - I thought my example was very straight forward. I'd explained that I had achived everything except the incremented numbers. I did supply an example of what I was after. I also did explain that had put the word End at the end of each paragraph and That all I was after was some help with the number incrementing. I don't know how much simpler I could have made it.

Marcus
 
put the word End at the end of each paragraph

That may seem clear to you, but when you are asking about code, basic needs analysis requires the question to be asked:

do you mean EVERY paragraph? do you mean EVERY NEW paragraph? Every EXISTING paragraph? Do you mean you have 1000 paragraphs, and then your macro is putting in 20 more, (but even that is not explained) and it is THOSE ones you want "End" and incremented numbers for. "Each" is a vague term, because it is expandable...there can be more later.

ALL is explicit. It means...all. No exceptions. If there is even one exception,
then the word ALL can not be used.

If you had stated that ALL paragraphs in the document had to have "End" plus the number, then it would have been clear. But you did not.

I made a macro which does other stuff as well, and it works, but I also need to put the word "End " at the end of each paragraph

Frankly, I can not see how you think the above is straightforward. "other stuff" "end of each paragraph" - each paragraph created by the macro? Each paragraph already in the document?

I am not trying to be difficult or rude. I am just trying to show that when one (anyone!) is looking at their stuff, they see THEIR stuff. They see their INTENTION, they see the problem.

We, out here, can not see anything at all unless someone tells us, or shows us code.

To repeat, I still have absolutely no idea if what you are trying to do is really put "End" and a number after every single paragraph in the document. I asked (and it really doesn't matter now, if you say what was posted helped...and I am happy it did!), but I still do not have an answer, I do NOT know if that is what was needed...because while you may think you have been straightforward...the bottom line to being clear and straightforward is the OTHER PERSON understanding, not you. And sorry, i still do not really know or understand.

But I AM glad my post could help.

BTW: this is my last post to Tek-Tips, except for a formal one saying goodbye to all you folks. Which i am just going to write now. Bye.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top