Number format of ranges using cell value

C

caroline

I have 6 named ranges (RangeX, RangeY, Etc)
I would like to format each range using a value in a cell (cell1, Cell2,
....Cell6)
For instance if cell1.value = 2 (meaning 2 decimal places)
then format of RangeX = "0.00"
I can write this separetely for each range but this is long and tedious. is
there a clever way to loop through this?
I tried but failed.
Any idea?
 
J

Jacob Skaria

Try..

Sub Macro()
Dim strNames As String, arrNames As Variant, intCount As Integer
Dim strCells As String, arrCells As Variant

strNames = "RangeX,RangeY,RangeZ"
strCells = "A1,A2,A3"

arrNames = Split(strNames, ",")
arrCells = Split(strCells, ",")

For intCount = 0 To UBound(arrNames)
Range(arrNames(intCount)).NumberFormat = _
"0." & String(Range(arrCells(intCount)), "0")
Next
End Sub
 
C

caroline

Totally brilliant. Thanks

--
caroline


Jacob Skaria said:
Try..

Sub Macro()
Dim strNames As String, arrNames As Variant, intCount As Integer
Dim strCells As String, arrCells As Variant

strNames = "RangeX,RangeY,RangeZ"
strCells = "A1,A2,A3"

arrNames = Split(strNames, ",")
arrCells = Split(strCells, ",")

For intCount = 0 To UBound(arrNames)
Range(arrNames(intCount)).NumberFormat = _
"0." & String(Range(arrCells(intCount)), "0")
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top