Format Dynamic Range

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good morning

trying to format multiple ranges. 1st range D6:G. the data will change (increase or decrease). the code I am using.

Thanks

Dim finalrow As Long
Dim i As Long

finalrow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 6 To finalrow

With Range("D6:G" & finalrow)
.NumberFormat = "0.0%"
.Value = Application.Evaluate("=" & .Address & "/" & 100)
.Font.Size = 22
End With

With Range("L6:O" & finalrow)
.NumberFormat = "0.0%"
.Value = Application.Evaluate("=" & .Address & "/" & 100)
.Font.Size = 22
End With


Next

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
So, what exactly is your question or issue?

Note that you do not need a FOR ... NEXT loop to format multiple rows, as you can apply the format to multiple rows at once, and your code is already set-up to do that.
 
Upvote 0
you answered my issue. I removed the For and the Next, seems to be working now.

Thank You
 
Last edited:
Upvote 0
Excellent!

Since you are defining the entire range like this:
Code:
[COLOR=#333333]With Range("D6:G" & finalrow)[/COLOR]
there is no need for a loop.
 
Upvote 0
I have been typically, (mostly through your direction) been using a loop. Which way is the most efficient?
 
Upvote 0
The other way.
Loops, by nature, are generally inefficient. So whenever there is an alternative, the alternative is usually more efficient.
 
Upvote 0
You are welcome.

I find myself doing loops a lot (maybe more than I should), mostly because I am pretty comfortable with them. As long as the data sets aren't huge, many times the differences aren't that noticeable. But I try to use the more efficient methods when I can.

Note that if you are using loops to update cells on your sheet, you can often improve their performance by temporarily shutting off screen updating while they are running, i.e.
Code:
Application.ScreenUpdating = False

' some loop here

Application.ScreenUpdating = True
Many times, using that little tip with loops can make a big difference.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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