Solved

Excel user defined function error

Posted on 2013-01-29
4
270 Views
Last Modified: 2013-02-01
Experts:

I need to determine if a certain cell is "BOLD"....  I've inserted the below code in an attempt; however, when I execute the call I get a compile error:  "User-defined type not defined"

Here is code...  was placed in a module

Public Function isBold(xlCell As cell) As Integer
  Dim isBold As Integer
  Dim bolTest As Integer
  bolTest = 0
  If xlCell.Font.Bold Then bolTest = 1
  isBold = bolTest
End Function

I also placed the code on the worksheet tab itself...  When placed there, I get nothing!

Thanks in advance for your assistance.

Rick Norris
0
Comment
Question by:Rick Norris
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 38831055
Replace as cell with As Range.
Public Function isBold(xlCell As Range) As Integer
  Dim isBold As Integer
  Dim bolTest As Integer
  bolTest = 0
  If xlCell.Font.Bold Then bolTest = 1
  isBold = bolTest
End Function

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38831236
Note that that function only evaluates whether the entire cell is set to bold.

It is also possible to set bold formatting on a character-by-character setting (thus allowing things like half of the text to be bold and the rest to be plain).

Do you need to test for that as well?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38831287
Two ways to handle testing not just entire cell, but also on a character basis:

Public Function isBold(xlCell As Range) As Boolean

    ' returns True if entire cell or any characters are set to bold,
    ' False otherwise
    
    Dim Counter As Long
    
    isBold = False
    
    With xlCell.Cells(1, 1)
        If .Font.Bold Then
            isBold = True
        Else
            For Counter = 1 To Len(.Value)
                If .Characters(Counter, 1).Font.Bold Then
                    isBold = True
                    Exit For
                End If
            Next
        End If
    End With
    
End Function

Public Function isBold2(xlCell As Range) As Long
    
    ' Returns:
    ' 0 = nothing at all in cell is bold
    ' 1 = entire cell set to bold
    ' 2 = at least one character set to bold
    
    Dim Counter As Long
    
    isBold2 = 0
    
    With xlCell.Cells(1, 1)
        If .Font.Bold Then
            isBold2 = 1
        Else
            For Counter = 1 To Len(.Value)
                If .Characters(Counter, 1).Font.Bold Then
                    isBold2 = 2
                    Exit For
                End If
            Next
        End If
    End With
    
End Function

Open in new window

0
 

Author Closing Comment

by:Rick Norris
ID: 38845033
Sorry for the delay,I got sidetracked the last few days.  Excellent catch in my code!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now