Consulting

Results 1 to 13 of 13

Thread: Selection.Font.Bold

  1. #1

    Selection.Font.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...

  2. #2
    what am i missing???

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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.
    K :-)

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why are you testing the current selection to see if it's bold?

  5. #5
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    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/

  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    @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 :-)

  7. #7
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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.

  8. #8
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    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/

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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
    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.

  10. #10
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    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!
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  11. #11
    that works...

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




  12. #12
    '... 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

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by crmpicco
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •