Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2024, 09:35 PM
syl3786 syl3786 is offline Find and replace the number format after vbTab Windows 10 Find and replace the number format after vbTab Office 2019
Advanced Beginner
Find and replace the number format after vbTab
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default Find and replace the number format after vbTab

Hello everyone,



I hope you're all doing well. I am currently working on a Word macro and I would appreciate your assistance with a specific task. My goal is to automatically identify whether the first character of paragraphs, particularly after a tab, is a number or not.

For instance, in the following example:

1 plus 1 is equal to more than two! As we begin the new year, let's take a moment to reflect on our accomplishments from the previous year and set our goals for the future. The first quarter of the year is crucial for our team as we have several important projects and deadlines to meet. It's vital to maintain open lines of communication among team members to ensure smooth collaboration and timely updates.

If a number [0-9] appears at the beginning of a paragraph, I would like to replace it with the corresponding word:

1 = one
2 = two
3 = three
4 = four

Since there may be other instances of numbers [0-9] in the document, a complete find and replace function is not suitable for this situation. Instead, the word macro should first identify the presence of a vbtab / Chr(9) before a number, and then determine if any [0-9] following the tab need to be replaced.

I have drafted the following code:

Code:
Sub ReplaceNumbersWithWords2()
    Dim para As Paragraph
    Dim text As String
    Dim i As Long

    For Each para In ActiveDocument.Paragraphs
        text = para.range.text
        
        If Left(text, 1) = Chr(9) Then
            i = 2
            While Mid(text, i, 1) >= "0" And Mid(text, i, 1) <= "9"
                Select Case Mid(text, i, 1)
                    Case "0"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "zero")
                    Case "1"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "one")
                    Case "2"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "two")
                    Case "3"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "three")
                    Case "4"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "four")
                    Case "5"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "five")
                    Case "6"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "six")
                    Case "7"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "seven")
                    Case "8"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "eight")
                    Case "9"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "nine")
                End Select
                
                i = i + 1
            Wend
        End If
    Next para
End Sub
I also hoped that the replaced numbers should be with track changes or in wdRed color.

May I ask if anyone can kindly assist me in achieving this task? Thank you very much in advance.
Reply With Quote
  #2  
Old 01-10-2024, 04:02 AM
syl3786 syl3786 is offline Find and replace the number format after vbTab Windows 10 Find and replace the number format after vbTab Office 2019
Advanced Beginner
Find and replace the number format after vbTab
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by syl3786 View Post
Hello everyone,

I hope you're all doing well. I am currently working on a Word macro and I would appreciate your assistance with a specific task. My goal is to automatically identify whether the first character of paragraphs, particularly after a tab, is a number or not.

For instance, in the following example:

1 plus 1 is equal to more than two! As we begin the new year, let's take a moment to reflect on our accomplishments from the previous year and set our goals for the future. The first quarter of the year is crucial for our team as we have several important projects and deadlines to meet. It's vital to maintain open lines of communication among team members to ensure smooth collaboration and timely updates.

If a number [0-9] appears at the beginning of a paragraph, I would like to replace it with the corresponding word:

1 = one
2 = two
3 = three
4 = four

Since there may be other instances of numbers [0-9] in the document, a complete find and replace function is not suitable for this situation. Instead, the word macro should first identify the presence of a vbtab / Chr(9) before a number, and then determine if any [0-9] following the tab need to be replaced.

I have drafted the following code:

Code:
Sub ReplaceNumbersWithWords2()
    Dim para As Paragraph
    Dim text As String
    Dim i As Long

    For Each para In ActiveDocument.Paragraphs
        text = para.range.text
        
        If Left(text, 1) = Chr(9) Then
            i = 2
            While Mid(text, i, 1) >= "0" And Mid(text, i, 1) <= "9"
                Select Case Mid(text, i, 1)
                    Case "0"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "zero")
                    Case "1"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "one")
                    Case "2"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "two")
                    Case "3"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "three")
                    Case "4"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "four")
                    Case "5"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "five")
                    Case "6"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "six")
                    Case "7"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "seven")
                    Case "8"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "eight")
                    Case "9"
                        para.range.text = Replace(para.range.text, Mid(text, i, 1), "nine")
                End Select
                
                i = i + 1
            Wend
        End If
    Next para
End Sub
I also hoped that the replaced numbers should be with track changes or in wdRed color.

May I ask if anyone can kindly assist me in achieving this task? Thank you very much in advance.
I solved it by myself with a general method.
Reply With Quote
  #3  
Old 01-10-2024, 05:23 AM
vivka vivka is offline Find and replace the number format after vbTab Windows 7 64bit Find and replace the number format after vbTab Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, syl3786! Or you can use:
Code:
Sub Repl_Digits_W__Wds_3()
'In selected paragraphs, replace the digits that start tabbed paras with wds.

Dim para As Paragraph
Dim dgt As range

Application.ScreenUpdating = False
    For Each para In selection.range.Paragraphs
'Search for only digits:
        If para.range.Characters(1) = Chr(9) And _
            para.range.Characters(2) Like "[0-9]" And _
            para.range.Characters(3) = Chr(32) Then
                Set dgt = para.range.Characters(2)
                Select Case dgt
                    Case "0"
                        dgt = "zero"
                    Case "1"
                        dgt = "one"
                    Case "2"
                        dgt = "two"
                    Case "3"
                        dgt = "three"
                    Case "4"
                        dgt = "four"
                    Case "5"
                        dgt = "five"
                    Case "6"
                        dgt = "six"
                    Case "7"
                        dgt = "seven"
                    Case "8"
                        dgt = "eight"
                    Case "9"
                        dgt = "nine"
                End Select
        End If
    Next para
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #4  
Old 01-10-2024, 08:17 AM
syl3786 syl3786 is offline Find and replace the number format after vbTab Windows 10 Find and replace the number format after vbTab Office 2019
Advanced Beginner
Find and replace the number format after vbTab
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Hi, syl3786! Or you can use:
Code:
Sub Repl_Digits_W__Wds_3()
'In selected paragraphs, replace the digits that start tabbed paras with wds.

Dim para As Paragraph
Dim dgt As range

Application.ScreenUpdating = False
    For Each para In selection.range.Paragraphs
'Search for only digits:
        If para.range.Characters(1) = Chr(9) And _
            para.range.Characters(2) Like "[0-9]" And _
            para.range.Characters(3) = Chr(32) Then
                Set dgt = para.range.Characters(2)
                Select Case dgt
                    Case "0"
                        dgt = "zero"
                    Case "1"
                        dgt = "one"
                    Case "2"
                        dgt = "two"
                    Case "3"
                        dgt = "three"
                    Case "4"
                        dgt = "four"
                    Case "5"
                        dgt = "five"
                    Case "6"
                        dgt = "six"
                    Case "7"
                        dgt = "seven"
                    Case "8"
                        dgt = "eight"
                    Case "9"
                        dgt = "nine"
                End Select
        End If
    Next para
Application.ScreenUpdating = True
End Sub
Thank you for your response. I appreciate your effort. However, I would like to bring to your attention that there seems to be an error "5941" when running the macro due to these three lines of code:

Code:
If para.range.Characters(1) = Chr(9) And _
 para.range.Characters(2) Like "[0-9]" And _
 para.range.Characters(3) = Chr(32) Then
I managed to resolve it by using vbTab instead of Chr(9) and by employing the use of case. Thank you for your attention to this matter.
Reply With Quote
  #5  
Old 01-10-2024, 08:24 AM
vivka vivka is offline Find and replace the number format after vbTab Windows 7 64bit Find and replace the number format after vbTab Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

It's strange, because I didn't have any problem with the code. Anyway, it's nice you have found a solution!
Reply With Quote
  #6  
Old 01-10-2024, 05:26 PM
Guessed's Avatar
Guessed Guessed is offline Find and replace the number format after vbTab Windows 10 Find and replace the number format after vbTab Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Here is a shorter option which works for me and applies the red colour as you requested.
Code:
Sub Repl_Digits_W__Wds_3()
  'In selected paragraphs, replace the digits that start tabbed paras with wds.
  Dim para As Paragraph, dgt As Range, arrText() As String
  
  arrText = Split("zero one two three four five six seven eight nine", " ")
  For Each para In Selection.Range.Paragraphs
    If para.Range.Text Like vbTab & "[0-9] *" Then    'Search for only single digits:
      Set dgt = para.Range.Characters(2)
      dgt.Text = arrText(dgt.Text)
      dgt.Font.ColorIndex = wdRed
    End If
  Next para
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 01-11-2024, 01:50 AM
vivka vivka is offline Find and replace the number format after vbTab Windows 7 64bit Find and replace the number format after vbTab Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Guessed, your code is the best solution, I think! And your "[0-9] *" is brilliant! I live and learn.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't find and replace (Appendix + any number) Muse1 Word 1 12-30-2023 08:36 AM
Find and replace the number format after vbTab find and replace with format karkey Word VBA 2 08-02-2022 03:04 PM
Find/Replace using format of cell catflap Excel 1 09-11-2017 07:28 AM
Find and replace the number format after vbTab Need Help to Find a Number and replace Allen Word 5 11-20-2014 10:33 PM
Find and replace the number format after vbTab Find and Replace maintain format winningson Word 3 01-19-2013 05:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft