#### ectoid

##### Board Regular
I am new as you will quickly learn. In the following code I ask the macro to evaluate column E for two conditions and then change the cell formatting of cells D, J, K, and M. However when I run it only K (offset (0,4)) is getting the cell format change, but none of the others.
Any Idea why?

'Set range variable to first Unit in Item list
Set c = Sheets("Estimate").Range("E7")

Do While c.Value <> ""

'Evalute Unit value to set number format and format cell of same row but offset column
If c.Value = "LS" Or c.Value = "FPLS" Then

Set fmtc = c.Offset(0, -1)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 3)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 4)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 6)
Selection.NumberFormat = "0.00%"

Else
End If

'Move variable range to next Unit in column
Set c = c.Offset(1, 0)

Loop

Thanks a bunch...

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Original:

'Set range variable to first Unit in Item list
Set c = Sheets("Estimate").Range("E7")

Do While c.Value <> ""

'Evalute Unit value to set number format and format cell of same row but offset column
If c.Value = "LS" Or c.Value = "FPLS" Then
'*Since you're selecting, and then doing something to the selection, these groups can be shortened to 1 line.
Set fmtc = c.Offset(0, -1)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 3)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 4)
Selection.NumberFormat = "0.00%"

Set fmtc = c.Offset(0, 6)
Selection.NumberFormat = "0.00%"
'** up to here
Else '*No need for an else
End If

'Move variable range to next Unit in column
Set c = c.Offset(1, 0)

Loop

This would shorten what you have to:
Code:
``````Sub test()
Dim c As Range, fmtc As Range

'Set range variable to first Unit in Item list
Set c = Sheets("Estimate").Range("E7")

Do While c.Value <> ""
'Evalute Unit value to set number format and format cell of same row but offset column
If c.Value = "LS" Or c.Value = "FPLS" Then
c.Offset(0, -1).NumberFormat = "0.00%"
c.Offset(0, 3).NumberFormat = "0.00%"
c.Offset(0, 4).NumberFormat = "0.00%"
c.Offset(0, 6).NumberFormat = "0.00%"
End If

'Move variable range to next Unit in column
Set c = c.Offset(1, 0)
Loop

End Sub``````

Seems to work OK for me. Also try stepping through your code with F8 to follow the flow of it. Hope that helps!

Hey thanks man...it works out great. What was confusing me too was a couple of hidden columns that was throwing off the offset...oops.
All good now though.

And thanks for the tip too.

You're most welcome, glad you got it worked out.

Replies
6
Views
157
Replies
2
Views
443
Replies
3
Views
496
Replies
0
Views
243
Replies
27
Views
530

1,219,672
Messages
6,149,617
Members
450,904
Latest member
Gracifer

### 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.

### Which adblocker are you using?

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

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