Mulit-column number formatting

M

MikeM

I'm trying to format a sequence of columns, but without success. I've used:

Sub x()

Dim i As Integer, iCol As Integer

For i = 1 To 47
iCol = 12 + 7 * (i - 1)
Range(Cells(, iCol), Cells(, iCol + 2)).NumberFormat = "0.00"
Next i
End Sub

I've tried columns(icol, icol+2) as well. Any ideas?

TIA,
Mike
 
D

Dave Peterson

How about:

Option Explicit
Sub x()

Dim i As Long
dim iCol As Integer

For i = 1 To 47
iCol = 12 + 7 * (i - 1)
activesheet.cells(1,icol).resize(1,2).entirecolumn.NumberFormat = "0.00"
Next i
End Sub

You must be using xl2007 for all those columns, right?
 
M

MikeM

Yes, I have Excel 2007. I tried your fixt, but it didn't seem to work. So
instead I used

Columns(iCol).NumberFormat = "0.00"
Columns(iCol + 1).NumberFormat = "0.00"
Columns(iCol + 2).NumberFormat = "0.00"

Why can't multiple columns be referenced?
 
D

Dave Peterson

I resized the range to 2 columns. I see that you wanted 3 columns.

Changing the .resize(1,2) to .resize(1,3) should work ok.

But I don't know why it didn't work for you. It worked fine for me in xl2003
(until I ran out of columns).

Maybe you could explain how it failed.
 

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