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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make replace function dynamic

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi!

I have a textbox consisting of a lot of text both numbers and chars.

Now in order to make the text dynamic i use replace on the text with certain variables, for example:

If the user needs the date 14 from now. I let him/her write #date14# and the program then searches the textbox for this and replaces it with date + 14 voila! Seemed pretty neat and ahs worked until now... Now we have expanded and a new need arose...
Dynamic date setting... Is it possible to have the program search for #date(whatever number)# store it in a variable. Then do the replace(text, date + the stored variable)?

Here's how i do the static replace
Code:
nytext = Replace(nytext, "#datum14#", Format(Date + 14, "long Date"))

I want it to be like this:

Code:
nytext = Replace(nytext, "#datum14#", Format(Date + (new-variable-with-dynamic-number), "long Date"))

I figured instr would help me and it does... But doing instring the cutting out the number seems like alot of job and a really bad way of solving the problem.. Also there are alot of problems, with ordinary find numbers in string functions cause they will find other numbers aswell.

Cheers!

 


hi,

Important to realize that the ONLY date manipulation is occurring in the Format conversion. Your ## delimiters are incorrect, as a "date" that is within a string, is just a STRING.

So your replacement is simply...
Code:
Dim nytext As String, datum14 As String, AddDays As Integer
nytext = "this is Monday, October 24, 2011, how great!"
datum14 = "Monday, October 24, 2011"
AddDays = 14
nytext = Replace(nytext, datum14, Format(Date + AddDays, "long Date"))


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
How about something like this (might be a little overkill but I do love a regex solution [smile])
Code:
Dim mystring As String
Dim m As Variant
mystring = "This is a #date14#, followed by a #date1#"

With CreateObject("VBScript.Regexp")

    .IgnoreCase = True
    .Global = True
    .MultiLine = True
    .Pattern = "(#date)([1-9]+)(#)"
    For Each m In .Execute(mystring)
        mystring = Replace(mystring, m.Value, Format(Date + CInt(m.SubMatches(1)), "long Date"))
    Next m
End With

MsgBox mystring
Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 



Guess it was not clear to me what nytext contains.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top