Solved

Excel user defined function error

Posted on 2013-01-29
4
272 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

21 Experts available now in Live!

Get 1:1 Help Now