Large find/replace in Word using VBA
up vote
2
down vote
favorite
I have a large amount of data from a hardware device that needs to be cleaned up so it can be analyzed in Excel.
I wrote a simple find/replace VBA macro in word to convert it to .csv, but the macro takes a long time to run through large sets of data.
I'm looking for suggestions on a faster way to code this. Alternately, if there is another option for cleaning the data that would work better than Word and VBA, I'm willing to switch to it.
Sub CleanText()
Application.ScreenUpdating = False
' clean data for import to excel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p" & "^p"
.Replacement.Text = "wvw"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "wvw"
.Replacement.Text = "^p"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPGGA" & "(*)" & "$GPRMC"
.Replacement.Text = "$GPRMC"
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "W" & "(*)" & "S"
.Replacement.Text = "W,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "V" & "(*)" & "S"
.Replacement.Text = "V,,,,,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPRMC,"
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.ScreenUpdating = True
End Sub
performance vba ms-word
add a comment |
up vote
2
down vote
favorite
I have a large amount of data from a hardware device that needs to be cleaned up so it can be analyzed in Excel.
I wrote a simple find/replace VBA macro in word to convert it to .csv, but the macro takes a long time to run through large sets of data.
I'm looking for suggestions on a faster way to code this. Alternately, if there is another option for cleaning the data that would work better than Word and VBA, I'm willing to switch to it.
Sub CleanText()
Application.ScreenUpdating = False
' clean data for import to excel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p" & "^p"
.Replacement.Text = "wvw"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "wvw"
.Replacement.Text = "^p"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPGGA" & "(*)" & "$GPRMC"
.Replacement.Text = "$GPRMC"
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "W" & "(*)" & "S"
.Replacement.Text = "W,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "V" & "(*)" & "S"
.Replacement.Text = "V,,,,,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPRMC,"
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.ScreenUpdating = True
End Sub
performance vba ms-word
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a large amount of data from a hardware device that needs to be cleaned up so it can be analyzed in Excel.
I wrote a simple find/replace VBA macro in word to convert it to .csv, but the macro takes a long time to run through large sets of data.
I'm looking for suggestions on a faster way to code this. Alternately, if there is another option for cleaning the data that would work better than Word and VBA, I'm willing to switch to it.
Sub CleanText()
Application.ScreenUpdating = False
' clean data for import to excel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p" & "^p"
.Replacement.Text = "wvw"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "wvw"
.Replacement.Text = "^p"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPGGA" & "(*)" & "$GPRMC"
.Replacement.Text = "$GPRMC"
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "W" & "(*)" & "S"
.Replacement.Text = "W,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "V" & "(*)" & "S"
.Replacement.Text = "V,,,,,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPRMC,"
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.ScreenUpdating = True
End Sub
performance vba ms-word
I have a large amount of data from a hardware device that needs to be cleaned up so it can be analyzed in Excel.
I wrote a simple find/replace VBA macro in word to convert it to .csv, but the macro takes a long time to run through large sets of data.
I'm looking for suggestions on a faster way to code this. Alternately, if there is another option for cleaning the data that would work better than Word and VBA, I'm willing to switch to it.
Sub CleanText()
Application.ScreenUpdating = False
' clean data for import to excel
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p" & "^p"
.Replacement.Text = "wvw"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ","
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "wvw"
.Replacement.Text = "^p"
.Forward = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPGGA" & "(*)" & "$GPRMC"
.Replacement.Text = "$GPRMC"
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "W" & "(*)" & "S"
.Replacement.Text = "W,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "V" & "(*)" & "S"
.Replacement.Text = "V,,,,,"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$GPRMC,"
.Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.ScreenUpdating = True
End Sub
performance vba ms-word
performance vba ms-word
edited Nov 17 '17 at 4:47
asked Nov 17 '17 at 4:22
gkavet
183
183
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You're doing a lot with .Selection. By default, that is going to be slow. Doing things behind the scenes is always faster. Either way, working with selection isn't the best way to go. Either pass the document to the sub or prompt the user. Is the selection a document, text, body, header, title, section? Be explicit.
The way you use .Find makes me thing you're referring to a range, which can be any of those things. Let's take a look at the basic skeleton of your sub -
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
End With
Yikes. You're doing the same thing 7 times. At worst that should be refactored. Something like this -
Private Sub AlterRange(ByVal target As Range, ByVal findString As String, ByVal replaceString As String)
With target
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Text findString
.Replacement replaceString
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
Now something like this would work
Dim target As Range
Set target = Selection
Dim findStrings(1 To 7) As String
findStrings(1) = "^p^p"
findStrings(2) = "^p"
findStrings(3) = "wvw"
findStrings(4) = "$GPGGA" & "(*)" & "$GPRMC"
'...
Dim replacestrings(1 To 7) As String
replacestrings(1) = "wvw"
replacestrings(2) = ","
'...
Dim i As Long
For i = 1 To 7
AlterRange target, findStrings(i), replacestrings(i)
Next
All of this is untested, but you get the idea.
What you could do to speed it up is to create a variant (or whatever) to store the entire selection in and then do your finding and replacing in the variant, then place the variant back out to the sheet overwriting the selection.
I don't know how all this would interact with your formatting, but it doesn't seem like you're keeping any formatting so it shouldn't be a problem.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You're doing a lot with .Selection. By default, that is going to be slow. Doing things behind the scenes is always faster. Either way, working with selection isn't the best way to go. Either pass the document to the sub or prompt the user. Is the selection a document, text, body, header, title, section? Be explicit.
The way you use .Find makes me thing you're referring to a range, which can be any of those things. Let's take a look at the basic skeleton of your sub -
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
End With
Yikes. You're doing the same thing 7 times. At worst that should be refactored. Something like this -
Private Sub AlterRange(ByVal target As Range, ByVal findString As String, ByVal replaceString As String)
With target
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Text findString
.Replacement replaceString
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
Now something like this would work
Dim target As Range
Set target = Selection
Dim findStrings(1 To 7) As String
findStrings(1) = "^p^p"
findStrings(2) = "^p"
findStrings(3) = "wvw"
findStrings(4) = "$GPGGA" & "(*)" & "$GPRMC"
'...
Dim replacestrings(1 To 7) As String
replacestrings(1) = "wvw"
replacestrings(2) = ","
'...
Dim i As Long
For i = 1 To 7
AlterRange target, findStrings(i), replacestrings(i)
Next
All of this is untested, but you get the idea.
What you could do to speed it up is to create a variant (or whatever) to store the entire selection in and then do your finding and replacing in the variant, then place the variant back out to the sheet overwriting the selection.
I don't know how all this would interact with your formatting, but it doesn't seem like you're keeping any formatting so it shouldn't be a problem.
add a comment |
up vote
0
down vote
You're doing a lot with .Selection. By default, that is going to be slow. Doing things behind the scenes is always faster. Either way, working with selection isn't the best way to go. Either pass the document to the sub or prompt the user. Is the selection a document, text, body, header, title, section? Be explicit.
The way you use .Find makes me thing you're referring to a range, which can be any of those things. Let's take a look at the basic skeleton of your sub -
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
End With
Yikes. You're doing the same thing 7 times. At worst that should be refactored. Something like this -
Private Sub AlterRange(ByVal target As Range, ByVal findString As String, ByVal replaceString As String)
With target
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Text findString
.Replacement replaceString
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
Now something like this would work
Dim target As Range
Set target = Selection
Dim findStrings(1 To 7) As String
findStrings(1) = "^p^p"
findStrings(2) = "^p"
findStrings(3) = "wvw"
findStrings(4) = "$GPGGA" & "(*)" & "$GPRMC"
'...
Dim replacestrings(1 To 7) As String
replacestrings(1) = "wvw"
replacestrings(2) = ","
'...
Dim i As Long
For i = 1 To 7
AlterRange target, findStrings(i), replacestrings(i)
Next
All of this is untested, but you get the idea.
What you could do to speed it up is to create a variant (or whatever) to store the entire selection in and then do your finding and replacing in the variant, then place the variant back out to the sheet overwriting the selection.
I don't know how all this would interact with your formatting, but it doesn't seem like you're keeping any formatting so it shouldn't be a problem.
add a comment |
up vote
0
down vote
up vote
0
down vote
You're doing a lot with .Selection. By default, that is going to be slow. Doing things behind the scenes is always faster. Either way, working with selection isn't the best way to go. Either pass the document to the sub or prompt the user. Is the selection a document, text, body, header, title, section? Be explicit.
The way you use .Find makes me thing you're referring to a range, which can be any of those things. Let's take a look at the basic skeleton of your sub -
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
End With
Yikes. You're doing the same thing 7 times. At worst that should be refactored. Something like this -
Private Sub AlterRange(ByVal target As Range, ByVal findString As String, ByVal replaceString As String)
With target
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Text findString
.Replacement replaceString
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
Now something like this would work
Dim target As Range
Set target = Selection
Dim findStrings(1 To 7) As String
findStrings(1) = "^p^p"
findStrings(2) = "^p"
findStrings(3) = "wvw"
findStrings(4) = "$GPGGA" & "(*)" & "$GPRMC"
'...
Dim replacestrings(1 To 7) As String
replacestrings(1) = "wvw"
replacestrings(2) = ","
'...
Dim i As Long
For i = 1 To 7
AlterRange target, findStrings(i), replacestrings(i)
Next
All of this is untested, but you get the idea.
What you could do to speed it up is to create a variant (or whatever) to store the entire selection in and then do your finding and replacing in the variant, then place the variant back out to the sheet overwriting the selection.
I don't know how all this would interact with your formatting, but it doesn't seem like you're keeping any formatting so it shouldn't be a problem.
You're doing a lot with .Selection. By default, that is going to be slow. Doing things behind the scenes is always faster. Either way, working with selection isn't the best way to go. Either pass the document to the sub or prompt the user. Is the selection a document, text, body, header, title, section? Be explicit.
The way you use .Find makes me thing you're referring to a range, which can be any of those things. Let's take a look at the basic skeleton of your sub -
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Find.Execute
End With
Yikes. You're doing the same thing 7 times. At worst that should be refactored. Something like this -
Private Sub AlterRange(ByVal target As Range, ByVal findString As String, ByVal replaceString As String)
With target
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find
.Text findString
.Replacement replaceString
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
Now something like this would work
Dim target As Range
Set target = Selection
Dim findStrings(1 To 7) As String
findStrings(1) = "^p^p"
findStrings(2) = "^p"
findStrings(3) = "wvw"
findStrings(4) = "$GPGGA" & "(*)" & "$GPRMC"
'...
Dim replacestrings(1 To 7) As String
replacestrings(1) = "wvw"
replacestrings(2) = ","
'...
Dim i As Long
For i = 1 To 7
AlterRange target, findStrings(i), replacestrings(i)
Next
All of this is untested, but you get the idea.
What you could do to speed it up is to create a variant (or whatever) to store the entire selection in and then do your finding and replacing in the variant, then place the variant back out to the sheet overwriting the selection.
I don't know how all this would interact with your formatting, but it doesn't seem like you're keeping any formatting so it shouldn't be a problem.
answered Mar 20 at 23:45
Raystafarian
5,7841047
5,7841047
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f180645%2flarge-find-replace-in-word-using-vba%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown