Loop

vidplaylist

Board Regular
Joined
Aug 3, 2011
Messages
78
Sales is already sorted with greater number and i want that sales greater than 50 get bold through "do while loop". I write this code but it's not working can someone please help with this.

Sub formatting1()
Dim Sales As Long
Sales = ActiveCell.Value
Do While Sales > 50
ActiveCell.Font.Bold = True
ActiveCell.Offset(1, 0).Activate


Loop
End Sub

Thank you,
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi

Perhaps:

Code:
Sub Test()
Dim r As Range, cell As Range
Set r = Range("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)  'assuming data in col A - amend as appropriate
For Each cell In r
  If cell.Value> 50 Then cell.Font.Bold = True
Next cell
End Sub
 
Upvote 0
Code:
Sub formatting2()
  r = ActiveCell.Row
  c = ActiveCell.Column
  lr = Cells(Rows.Count, c).End(xlUp).Row
  Do While Cells(r, c).Value > 50 Or r > lr
    r = r + 1
  Loop
  Range(ActiveCell, Cells(r, c)).Font.Bold = True
End Sub
 
Upvote 0
Richards,

Can you please explain the meaning of this part

("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)

Thank you,
 
Upvote 0
Weaver,

Can you please explain the meaning of this statement

lr = Cells(Rows.Count, c).End(xlUp).Row

Thank you,
 
Upvote 0
Richards,

Can you please explain the meaning of this part

("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)

Thank you,

Sets the range to be checked (and bolded if value is greater than 50). The Cells(Rows.Count,"A").End(xlUp).Row returns the row of the last line of data by going to the last row in column A and (effectively) pressing Ctrl+Up Arrow to find the last row.
 
Upvote 0
Weaver,

Can you please explain the meaning of this statement

lr = Cells(Rows.Count, c).End(xlUp).Row

Thank you,
See Richard's explanation, it's a slightly different play on the same principle.

Knowing where the data ends can be useful sometimes, as it stops you testing cells after there data runs out.

Overall, Richard's solution might be better in that it doesn't depend on the results to be sorted for it to work correctly.

Alternatively, have you considered using conditional formatting?
 
Upvote 0
Hi

Perhaps:

Code:
Sub Test()
Dim r As Range, cell As Range
Set r = Range("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)  'assuming data in col A - amend as appropriate
For Each cell In r
  If cell.Value> 50 Then cell.Font.Bold = True
Next cell
End Sub

Richard,

This works for me but I would like to BOLD the row from A:O for each row that the cell in Col D equals "Total"

Help? (THANKS!!)
 
Upvote 0
Hi Carole

Try this:

Rich (BB code):
Sub Test()
Dim r As Range, cell As Range
Set r = Range("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)  'assuming data in col A - amend as appropriate
For Each cell In r
  If cell.Offset(0,3).Value> 50 Then cell.Resize(,15).Font.Bold = True
Next cell
End Sub

Relevant amendments are in red
 
Upvote 0
Hi Carole

Try this:

Rich (BB code):
Sub Test()
Dim r As Range, cell As Range
Set r = Range("A2:A" & Cells(Rows.Count,"A").End(xlUp).Row)  'assuming data in col A - amend as appropriate
For Each cell In r
  If cell.Offset(0,3).Value = "Total" Then cell.Resize(,15).Font.Bold = True
Next cell
End Sub

Relevant amendments are in red


PERFECT! Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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