Help Troubleshoot this...please

ectoid

Board Regular
Joined
Jan 18, 2005
Messages
97
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
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!
 

ectoid

Board Regular
Joined
Jan 18, 2005
Messages
97
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.
 

Forum statistics

Threads
1,147,677
Messages
5,742,566
Members
423,738
Latest member
AshleyKitsune

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
Top