Macro to look text in the right column and set a variable character count

Bekerinik

New Member
Joined
Oct 23, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello! I have this macro that highlights in blue the text in the last column of the tables in the file if it exceeds the 100 characters. I would need to edit it so that the char count changes based on the text in the first column of each row. For example:
1729689991838.png

The macro does not highlight "Body text" because it sees "250 char" in the left column, sets the char count to 250 and and counts that "Body text" is shorter.
It goes to the next row and sees "20 char" in the left column, sets the char count to 20 for the right column and highlights the text.

This is the macro that I have now:
VBA Code:
Sub HighlightLongRightColumnParagraphs()
    Dim tbl As Table
    Dim cell As Cell
    Dim para As Paragraph
    Dim charCount As Integer
    charCount = 100

    For Each tbl In ActiveDocument.Tables
        ' Get the last column index
        Dim lastColIndex As Integer
        lastColIndex = tbl.Columns.Count
       
        ' Loop through each cell in the last column
        For Each cell In tbl.Columns(lastColIndex).Cells
            For Each para In cell.Range.Paragraphs
                If Len(para.Range.Text) > charCount Then
                    para.Range.HighlightColorIndex = wdTurquoise
                End If
            Next para
        Next cell
    Next tbl
End Sub

Can you help me? I get stuck with the left-right cell part.

Thanks!
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
As always, a description of what is constant about the seed data should be given. Put another way, your seed data is whatever contains (or just is) the number you need. In your examples that is 250 and 20. However, to find those numbers within a string requires knowing what is stable about the string. I'm going to guess that it is constant that the number will follow the left parenthesis. So if the data was
Rich (BB code):
TEXT(20 Char)
then this would return 20.

Rich (BB code):
val(mid("TEXT(20 Char)",instr("TEXT(20 Char)","(")+1))

If your number contained comma separators (e.g. 1,000) that would not work as is.
Please enclose code within code tags (use vba button on posting toolbar) to maintain indentation and readability. I used rich text tags here because the forum changed my ( to emojis.
 
Last edited:
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top