PDA

View Full Version : [SOLVED] Selection.Font.Bold



crmpicco
05-27-2005, 07:29 AM
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...

crmpicco
05-27-2005, 07:36 AM
what am i missing???

Killian
05-27-2005, 07:46 AM
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


If Trim(sRange.Text) <> "" And ActiveCell.Font.Bold = False Then
sRange.Interior.ColorIndex = iColour
End If

should be fine.

Norie
05-27-2005, 09:37 AM
Why are you testing the current selection to see if it's bold?

TheAntiGates
05-27-2005, 09:59 AM
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, :bow: )

Killian
05-27-2005, 07:28 PM
@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...

johnske
05-27-2005, 08:20 PM
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

TheAntiGates
05-27-2005, 08:54 PM
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?

johnske
05-27-2005, 09:31 PM
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 Then
Range(sRange).Interior.ColorIndex = iColour
End If

you can also use the following with & without a space in A1 as an experiment


Option Explicit
Sub try()
If [a1] <> Empty Then MsgBox [a1]
End Sub


Regards,
John

TheAntiGates
05-27-2005, 10:04 PM
I'm saying that some coders might go


Len(Trim(Range(sRange).Text)) <> 0

instead of


Trim(Range(sRange).Text) <> ""

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! :)

crmpicco
05-31-2005, 07:02 AM
that works...



If Trim(Range(sRange).Text) <> "" And Range(sRange).Font.Bold = False Then
Range(sRange).Interior.ColorIndex = iColour
End If

crmpicco
05-31-2005, 08:49 AM
'... 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

Bob Phillips
05-31-2005, 08:57 AM
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 values

XlUnderlineStyle can be one of these XlUnderlineStyle constants.
xlUnderlineStyleNone
xlUnderlineStyleSingle

xlUnderlineStyleDouble

xlUnderlineStyleSingleAccounting

xlUnderlineStyleDoubleAccounting

so check for xlUnderlineStyleNone or not