I'm working on a Microsoft Word Macro that aims to perform the following tasks:
1) Check if the 000000 in the second column of a Word Document are in increasing order.
2) If not, change the font colour of the wrong 6-digit number to wdred and track the changes.
3) Check if the digit numbers in the second column of a Word Document are 000000. If not, change them to wdred and track the changes.
4) If any changes have been made, a message box will pop up saying, "Some of them are not in an increasing order."
4a) If no changes have been made, a message box will pop up saying, "No mistake is found."
Now there are two macros to complete the above tasks. Can they merge as one macro? I tried but failed due to some technical reasons.
For Checking the numbers are in ascending order:
I have drafted the code as follows but with bugs:
- sometimes it can identify the wrong 6-digit numbers(not in ascending order) but sometimes cannot
- sometimes if the wrong numbers are found, those 6-digit numbers after the wrong one will also be marked in wdRed.
- How can it just find and replace the numbers in the second column of the table?
Code: Select all
Sub CheckNo1()
Dim doc As Document
Dim rng As Range
Dim num As String
Dim prevNum As Long
Dim sMade As Boolean
Set doc = ActiveDocument
Set rng = doc.Content
With rng.Find
.ClearFormatting
.text = "([0-9]{6})"
.MatchWildcards = True
.Execute
Do While .found
num = .Parent.text
If IsNumeric(num) Then
If CLng(num) <= prevNum Then
.Parent.Font.Color = RGB(128, 200, 200)
sMade = False
Else
prevNum = CLng(num)
End If
End If
.Execute
Loop
End With
If sMade Then
MsgBox Prompt:="Please check the red-marked numbers"
Else
MsgBox Prompt:="No mistake."
End If
End If
End Sub
Code: Select all
Sub CheckNo2()
Dim doc As Document
Dim rng As Range
Dim sMade As Boolean
Set rng = ActiveDocument.Content
sMade = False
With rng.Find
.ClearFormatting
.text = "\[[0-9]{4}\]"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
rng.Font.Color = RGB(128, 0, 0)
rng.Collapse wdCollapseEnd
sMade = True
Loop
End With
With rng.Find
.ClearFormatting
.text = "\[[0-9]{5}\]"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
rng.Font.Color = RGB(128, 0, 0)
rng.Collapse wdCollapseEnd
sMade = True
Loop
End With
With rng.Find
.ClearFormatting
.text = "\[[0-9]{7}\]"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
rng.Font.Color = RGB(128, 0, 0)
rng.Collapse wdCollapseEnd
sMade = True
Loop
End With
With rng.Find
.ClearFormatting
.text = "\[[0-9]{8}\]"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
rng.Font.Color = RGB(128, 0, 0)
rng.Collapse wdCollapseEnd
sMade = True
Loop
End With
With rng.Find
.ClearFormatting
.text = "\[[0-9]{9}\]"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
rng.Font.Color = RGB(128, 0, 0)
rng.Collapse wdCollapseEnd
sMade = True
Loop
End With
If sMade Then
MsgBox Prompt:="Please check the red-marked numbers."
Else
MsgBox Prompt:="No mistake."
End If
End If
End Sub