VBA Code for formatting to stop when it reaches a WRAP text range

peter8848

Board Regular
Joined
Oct 7, 2018
Messages
112
Hi All,

I developed a VBA code for formatting my excel reports say from Column B to D, F to G to change the font. color etc. As this stage I am doing a certain range say from Row 11 to 195 to avoid any Wrap text area as if i just select as Range("B:D,F:G").Select, then it will format more columns and rows than i needed to cause messy reporting format. Is there anyway to write a VBA code to stop the formatting to a particular row before reaching a wrap text area? So in my example the wrap text happened in Row 196 which I need to manually change my code. thanks a lot


Range("B11").Select
Range("B11:D195,F11:G195").Select
Selection.NumberFormat = "#,##0_);[Red](#,##0)"
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Not to sure how you want to loop through these columns but here is a code that will stop the code with a message box saying "This cell has Wrap Text" and the cell address.

VBA Code:
Sub Macro1()
Dim lastRow As Long, cl As Object, strCells As String
With Sheets("Sheet1")
    lastRow = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
    strCells = "B11:B" & lastRow
    For Each cl In .Range(strCells)
        If cl.WrapText = True Then
            MsgBox "This cell has Wrap Text " & cl.Address
            Exit Sub
        End If
    Next cl
End With
End Sub
 
Upvote 0
Not to sure how you want to loop through these columns but here is a code that will stop the code with a message box saying "This cell has Wrap Text" and the cell address.

VBA Code:
Sub Macro1()
Dim lastRow As Long, cl As Object, strCells As String
With Sheets("Sheet1")
    lastRow = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
    strCells = "B11:B" & lastRow
    For Each cl In .Range(strCells)
        If cl.WrapText = True Then
            MsgBox "This cell has Wrap Text " & cl.Address
            Exit Sub
        End If
    Next cl
End With
End Sub


Hi VBE313,

Thanks a lot for your reply. Sorry how would the best way to group your code into mine for the formatting part?

Cheers,

Peter
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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