Randy413
02-18-2015, 02:12 PM
Using Office 2007;
I have a word table that I use to track stock values. The table has a list of stock symbols that I need to occasionally change.
I am using vba to find and replace the symbols in the table. Right now I use "inputbox" to list the existing symbols in the table and again to list the new symbols. The code works fine, however, I would like vba to find the existing symbols in the table and I will continue to use the "inputbox" to change the list. The tables are single array and can vary from 3 rows to 12 rows determined by my old method of inputting the symbols manually. VBA would need to count the number of rows used in each table from 3 to 12 (each table has only 3, 4, 5, ...12 rows etc)
Here is my code:
Sub replaceSymbols()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
' the code between the = signs is what I would like to automate. I.E. vba would identify
' the current (oldSym) symbols and enter them in the code here: .Text = Split(oldSym, ",")(i)
' and I will continue to manually enter the new symbols in: newSym=Imputbox...
'========
oldSym = InputBox(prompt:="Old Symbols", Title:="List old symbols separated by commas")
'========
newSym = InputBox(prompt:="New Symbols", Title:="Replace with new symbols separated by commas")
For i = 0 To UBound(Split(oldSym, ","))
Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Text = Split(oldSym, ",")(i)
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = Split(newSym, ",")(i)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
.Execute Replace:=wdReplaceAll
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
I have a word table that I use to track stock values. The table has a list of stock symbols that I need to occasionally change.
I am using vba to find and replace the symbols in the table. Right now I use "inputbox" to list the existing symbols in the table and again to list the new symbols. The code works fine, however, I would like vba to find the existing symbols in the table and I will continue to use the "inputbox" to change the list. The tables are single array and can vary from 3 rows to 12 rows determined by my old method of inputting the symbols manually. VBA would need to count the number of rows used in each table from 3 to 12 (each table has only 3, 4, 5, ...12 rows etc)
Here is my code:
Sub replaceSymbols()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
' the code between the = signs is what I would like to automate. I.E. vba would identify
' the current (oldSym) symbols and enter them in the code here: .Text = Split(oldSym, ",")(i)
' and I will continue to manually enter the new symbols in: newSym=Imputbox...
'========
oldSym = InputBox(prompt:="Old Symbols", Title:="List old symbols separated by commas")
'========
newSym = InputBox(prompt:="New Symbols", Title:="Replace with new symbols separated by commas")
For i = 0 To UBound(Split(oldSym, ","))
Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Text = Split(oldSym, ",")(i)
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = Split(newSym, ",")(i)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
.Execute Replace:=wdReplaceAll
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub