PDA

View Full Version : Solved: How to get user input?



Rakesh
08-04-2010, 02:44 PM
:banghead:
Hi I have the following Codes to replace some text

The Replacewith Text (digit) should vary from time to time i.e., “Tip 2(5,7):”
So How to get input from the user? For example 6,5.
And to change it in the replacewith text. “Tip 2(6,5):"

Sub replace()
Selection.HomeKey Unit:=wdStory
ActiveDocument.Content.Find.Execute _
FindText:="([!^13]@%^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, _
ReplaceWith:="Tip 1:\1Tip 2(5,7):\2Tip 3:\3", _
Replace:=wdReplaceAll
End Sub


Thanks
Rakesh

gmaxey
08-04-2010, 04:07 PM
Use something like this:

Sub replace()
Dim pReplaceSubSet1 As String
Dim pReplaceSubSet2 As String
Dim pReplaceSubSet3 As String
Dim pReplaceSet As String
pReplaceSubSet1 = "Tip 1:\1Tip 2("
pReplaceSubSet2 = "5,7"
pReplaceSubSet3 = "):\2Tip 3:\3"
If MsgBox("The current Tip 2 replacement set is ""5,7"". Do you want to change it?", vbQuestion + vbYesNo, "Tip 2 Set") = vbYes Then
pReplaceSet = pReplaceSubSet1 + InputBox("Enter the Tip 2 replacement set (e.g., 6,5).", "Tip 2 Set") + pReplaceSubSet3
Else
pReplaceSet = pReplaceSubSet1 + pReplaceSubSet2 + pReplaceSubSet3
End If
MsgBox pReplaceSet
Selection.HomeKey Unit:=wdStory
ActiveDocument.Content.Find.Execute _
FindText:="([!^13]@%^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, ReplaceWith:=pReplaceSet, replace:=wdReplaceAll
End Sub

Rakesh
08-04-2010, 04:38 PM
Great Job gmaxey


Its working charmly

Thanks a ton

Rakesh
08-07-2010, 11:56 AM
Hi gmaxey,

One more Help needed.

How to get the Second digit Value (i.e., 5) to the Second Sub macro.

The Replacewith Text (digit) should vary from time to time i.e., “Tip 2(5,7):”
So How to get input from the user? For example 6,5
And to change it in the replacewith text. “Tip 2(6,5):"

Sub replace()
Selection.HomeKey Unit:=wdStory
ActiveDocument.Content.Find.Execute _
FindText:="([!^13]@%^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, _
ReplaceWith:="Tip 1:\1Tip 2(5,7):\2Tip 3:\3", _
Replace:=wdReplaceAll

ActiveDocument.Content.Find.Execute _
FindText:="(^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, _
ReplaceWith:="\1Tip 4:(,7) entry:\2Tip 5:\3", _
Replace:=wdReplaceAll
End Sub

Thanks
Rakesh

gmaxey
08-07-2010, 03:04 PM
This is getting sort of convoluted but I don't really know exactly what yoru are trying to do. Try:

Sub replace()
Dim pReplaceSubSet1 As String
Dim pReplaceSubSet2 As String
Dim pReplaceChangedSubSet2 As String
Dim pReplaceSubSet3 As String
Dim pReplaceSet As String
Dim bChanged As Boolean
Dim i As Long
bChanged = False
pReplaceSubSet1 = "Tip 1:\1Tip 2("
pReplaceSubSet2 = "5,7"
pReplaceSubSet3 = "):\2Tip 3:\3"
If MsgBox("The current Tip 2 replacement set is ""5,7"". Do you want to change it?", vbQuestion + vbYesNo, "Tip 2 Set") = vbYes Then
bChanged = True
End If
If bChanged Then
pReplaceChangedSubSet2 = InputBox("Enter the Tip 2 replacement set (e.g., 6,5).", "Tip 2 Set")
pReplaceSet = pReplaceSubSet1 + pReplaceChangedSubSet2 + pReplaceSubSet3
Else
pReplaceSet = pReplaceSubSet1 + pReplaceSubSet2 + pReplaceSubSet3
End If
Selection.HomeKey Unit:=wdStory
ActiveDocument.Content.Find.Execute _
FindText:="([!^13]@%^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, ReplaceWith:=pReplaceSet, replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
pReplaceSubSet2 = ",7"
If bChanged Then
i = InStr(pReplaceChangedSubSet2, ",")
pReplaceChangedSubSet2 = Mid(pReplaceChangedSubSet2, i, Len(pReplaceChangedSubSet2) - i + 1)

pReplaceSet = pReplaceSubSet1 + pReplaceChangedSubSet2 + pReplaceSubSet3
Else
pReplaceSet = pReplaceSubSet1 + pReplaceSubSet2 + pReplaceSubSet3
End If
ActiveDocument.Content.Find.Execute _
FindText:="(^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, _
ReplaceWith:="\1Tip 4:(,7) entry:\2Tip 5:\3", _
replace:=wdReplaceAll
End Sub

Rakesh
08-07-2010, 04:30 PM
Hi Greg

Thanks for your try. I thought I failed to explain you clearly.

If the User entered 9,14 in the Input Box, 9,14 should be replaced in the First sub macro and the value 14 should be replaced in the Second sub macro

If you go through the following Sample Data and Output you will be cleared.

Sample DATA

Adelaide Brighton Ltd. 25,069
Antartica - 0.9%
3,411,868
Austria - 0.9%
109 Agrana Beteiligungs AG 10,342
570 Andritz AG 31,630
339 Semperit AG Holding 11,964
409 Zumtobel AG* 7,109
270,885
Belgium - 1.3%
170 Ackermans<\!s>& van Haaren NV 12,366

OUTPUT

10,842 ABC Learning Centres Ltd.* <\#208>
Adelaide Brighton Ltd. 25,069
Tip 1:Antartica - 0.9%
Tip 2(9,14):3,411,868
Tip 3:Austria - 0.9%
109 Agrana Beteiligungs AG 10,342
570 Andritz AG 31,630
339 Semperit AG Holding 11,964
409 Zumtobel AG* 7,109
Tip 4(,14) entry:270,885
Tip 5:Belgium - 1.3%
170 Ackermans<\!s>& van Haaren NV 12,366


Thanks
Rakesh

gmaxey
08-07-2010, 06:22 PM
Sub replace()
Dim pReplaceSubSet1 As String
Dim pReplaceSubSet2 As String
Dim pReplaceChangedSubSet2 As String
Dim pReplaceSubSet3 As String
Dim pReplaceSet As String
Dim bChanged As Boolean
Dim i As Long
bChanged = False
pReplaceSubSet1 = "Tip 1:\1Tip 2("
pReplaceSubSet2 = "5,7"
pReplaceSubSet3 = "):\2Tip 3:\3"
If MsgBox("The current Tip 2 replacement set is ""5,7"". Do you want to change it?", vbQuestion + vbYesNo, "Tip 2 Set") = vbYes Then
bChanged = True
End If
If bChanged Then
pReplaceChangedSubSet2 = InputBox("Enter the Tip 2 replacement set (e.g., 6,5).", "Tip 2 Set")
pReplaceSet = pReplaceSubSet1 + pReplaceChangedSubSet2 + pReplaceSubSet3
Else
pReplaceSet = pReplaceSubSet1 + pReplaceSubSet2 + pReplaceSubSet3
End If
Selection.HomeKey Unit:=wdStory
ActiveDocument.Content.Find.Execute _
FindText:="([!^13]@%^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, ReplaceWith:=pReplaceSet, replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
pReplaceSubSet2 = ",7"
If bChanged Then
i = InStr(pReplaceChangedSubSet2, ",")
pReplaceChangedSubSet2 = Mid(pReplaceChangedSubSet2, i, Len(pReplaceChangedSubSet2) - i + 1)

pReplaceSet = pReplaceSubSet1 + pReplaceChangedSubSet2 + pReplaceSubSet3
Else
pReplaceSet = pReplaceSubSet1 + pReplaceSubSet2 + pReplaceSubSet3
End If
ActiveDocument.Content.Find.Execute _
FindText:="(^13)([0-9,]@^13)([!^13]@%^13)", _
MatchWildcards:=True, _
ReplaceWith:=pReplaceSet, _
replace:=wdReplaceAll
End Sub

Rakesh
08-10-2010, 08:32 AM
Hi Greg,

Once again Thanks a Lot


:rotlaugh:
Rakesh