If Trim(Range(sRange).Text) <> "" And Selection.Font.Bold = False Then Range(sRange).Interior.ColorIndex = iColour End If
this code doesnt work or if the range is not blank and if the text is not bold...
If Trim(Range(sRange).Text) <> "" And Selection.Font.Bold = False Then Range(sRange).Interior.ColorIndex = iColour End If
this code doesnt work or if the range is not blank and if the text is not bold...
what am i missing???
What doesn't work (error or unexpected result)?
Assuming sRange is defined as a range and your selection is a cell (or range of cells) then
should be fine.If Trim(sRange.Text) <> "" And ActiveCell.Font.Bold = False Then sRange.Interior.ColorIndex = iColour End If
K :-)
Why are you testing the current selection to see if it's bold?
Killian,
Clearly what we have here is a failure to communicate ... but he probably was right with the use of sRange. In Hungarian Notation s refers to strings, i to integers, etc., and I'd bet that he's using that convention. Even if he doesn't realize it.
When I'm disciplined enough to follow my own advice, I use rng as a prefix for ranges, though I don't know if any "official" standard exists for it. (But if it's good enough for j-walk, )
I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/
@AntiGates
yes good point, it could indeed be a range name string, in which case the OP's code seems to work fine.
Unless sRange doesn't describe a valid range or the selection doesn't have a Font property... either would produce an error...
K :-)
Don't you mean something like this?..
If Trim(Range(sRange).Text) <> "" And Range(sRange).Font.Bold = False Then Range(sRange).Interior.ColorIndex = iColour End If
You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you
The major part of getting the right answer lies in asking the right question...
Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.
Good catch, John ... you must have talent with the Scotch (or is it Scottish?!) dialect.
A side note, since OP seems to be whatever ... I have seen credible sources suggest testing len vs. 0 instead of testing "" . Would that be less susceptible to a RTE?
I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/
Hi AntiBill,
(I'm not quite sure what's being tested for in the above code, I'd have to see an example). And I'm not sure what your question is in reference to either. But, where possible my personal preference is to use Empty over "" (Empty also caters for spaces) E.G. in the above case, perhaps the OP meant something like...
If Range(sRange) <> Empty And Range(sRange).Font.Bold = False ThenRange(sRange).Interior.ColorIndex = iColour End If
you can also use the following with & without a space in A1 as an experiment
Option ExplicitRegards,Sub try() If [a1] <> Empty Then MsgBox [a1] End Sub
John
You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you
The major part of getting the right answer lies in asking the right question...
Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.
I'm saying that some coders might go
instead ofLen(Trim(Range(sRange).Text)) <> 0
My recollection was to wonder whether RTE (run time errors) would be more or less likely with a particular approach. Just thinking out loud here: many of us have had cases where we had to break up a compound IF statement (MS with its foolish order of evaluation!) and use IsNull in the first test to avoid a RTE. So I was just wondering out loud whether using len was preferable - or not. If you know a quick way to construct a test where Trim(Range(sRange).Text) is empty or null, please go nuts!Trim(Range(sRange).Text) <> ""
I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/
that works...
If Trim(Range(sRange).Text) <> "" And Range(sRange).Font.Bold = False Then Range(sRange).Interior.ColorIndex = iColour End If
'... if the heading is not blank and the two left-most characters are Upper Case If Trim(Range(sRange).Text) <> "" And Range(sRange).Font.Underline = False Then Range(sRange).Interior.ColorIndex = iColour End If
Is this not valid? i ain;t working
If you look at help you will see that underline is not a simple True/false, it has many valuesOriginally Posted by crmpicco
XlUnderlineStyle can be one of these XlUnderlineStyle constants.
xlUnderlineStyleNone
xlUnderlineStyleSingle
xlUnderlineStyleDouble
xlUnderlineStyleSingleAccounting
xlUnderlineStyleDoubleAccounting
so check for xlUnderlineStyleNone or not