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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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!
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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