Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel user defined function error

Posted on 2013-01-29
4
Medium Priority
?
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Norie earned 2000 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 93

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 93

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

670 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