![]() |
#38
|
||||
|
||||
![]()
Well, here are a number of disconnected comments. I don't expect they're exhaustive; there's probably more to talk about. But they're what I see off the top of my head:
1) You say it applied the conditional formatting to the whole spreadsheet. I can tell that from here: You applied it to Selection, and you set the selection to Cells. Here's one of those fundamentals I want you to understand: When you name an object, if you don't name its parent you subject yourself to Excel's notions of defaults. As long as you know what the defaults are, that's ok; you're doing it with your eyes open. Cells, for example, means all the cells in a particular worksheet. (That is, Cells by itself means that. I usually specify a particular cell by referring to, eg, Cells(2, 5); that means the cell in row 2, column 5, ie E2.) But which worksheet? I believe the default is ActiveWorksheet, though someone else here may contradict me. I say "I believe" because in practice I almost always specify. ActiveWorksheet, fine—but of which workbook? Again, I believe it's ActiveWorkbook, though it may be ThisWorkbook. So in my own programs I almost always start out like this: Code:
Set owb = ThisWorkbook Set owss = owb.Worksheets Set owsFrom = owss("Worksheet name #1") Set owsTo = owss("Worksheet name #2") Anyway, back to the subject: You said "Cells.Select", which set the selection to all the cells in the active worksheet, and then started working with Selection.FormatConditions. So naturally what you did applied to the whole worksheet. If you wanted to set the formatting for just A1:L<whatever>, then set rz before you do the formatting: Code:
rz = Range("L1").End(xlDown).Row Range("A1:L" & rz).Select Selection.EntireColumn.Autofit Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$B2=1" By the way, I wouldn't use Select, either; I'd set a variable. That way I can't accidentally change the Selection and have it change the way my program works. Like this: Code:
rz = Range("L1").End(xlDown).Row Set org = Range("A1:L" & rz) org.EntireColumn.Autofit org.FormatConditions.Add Type:=xlExpression, Formula1:="=$B2=1" 2) I don't think the problem is that you selected the whole worksheet for your conditional formatting. Oh, it was a mistake, but only in the sense that you didn't mean to do it; the conditional format should still have worked. Must be something else. You say it worked for only some rows, so let's take a look at the statements you used. First: Code:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$B2=1" Code:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=RC2=1" Code:
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority But then you go on to work with the first conditional format: Code:
With Selection.FormatConditions(1).Font . . . Selection.FormatConditions(1).StopIfTrue = False To investigate why your conditional formatting wasn't applied to all the relevant rows, I suggest modifying your code match your new understanding...whatever new understanding I've managed to convey to you :-)...and then, if it still doesn't work, I'll need to know exactly which rows didn't work. I'll probably need to see the worksheet itself, to be sure; can you upload it, or are there classification issues? |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formatting contents after Tab of continuous lines or formatting specific area of word | pawii | Word | 1 | 05-12-2014 05:24 AM |
macros | stebrownsword | Word VBA | 0 | 08-28-2013 01:16 AM |
![]() |
anju16saini | Word VBA | 1 | 03-11-2013 04:15 AM |
![]() |
WaltR | Word VBA | 8 | 05-15-2012 06:28 PM |
Macros | nore | Outlook | 0 | 06-01-2011 04:39 PM |