+ Reply to Thread
Results 1 to 4 of 4

I can use VBA to create a text box, how do I set the background colour?

Hybrid View

irf90 I can use VBA to create a... 09-29-2014, 05:26 PM
Leith Ross Re: I can use VBA to create a... 09-29-2014, 06:30 PM
irf90 Re: I can use VBA to create a... 09-30-2014, 08:53 AM
Leith Ross re: I can use VBA to create a... 09-30-2014, 10:42 AM
  1. #1
    Registered User
    Join Date
    03-02-2007
    Posts
    2

    I can use VBA to create a text box, how do I set the background colour?

    Firstly I'm new to VBA, but trying to learn to make Excel more useful in work.

    My company is using Excel 2003(!) and I'm trying to create custom buttons which add text boxes to selected cells - I want to create a series of buttons to create different coloured text boxes. Through lots of google searching, and some background reading I've managed to come up with the following code to create a text box the size of selected cells

    Sub TextBox()    
         If TypeName(Selection) = "Range" Then
            With Selection
                ActiveSheet.Shapes.AddTextbox _
                  msoTextOrientationHorizontal, .Left, _
                  .Top, .Width, .Height
            End With
        End If
    End Sub
    Despite numerous tries, I can't figure out what code, or where to place it to set parameters such as font size, alignment and fill colour for the text box. Any help greatly appreciated.
    Last edited by irf90; 09-30-2014 at 10:35 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: I can use VBA to create a text box, how do I set the background colour?

    Hello irf90,

    Welcome to the Forum!

    Here is a macro to change some the properties of the Text Box. There are more. To see them you need to view them in the Object Browser for the VB Editor.
    Sub Macro1()
    
        Dim Msg As String
        Dim TB As Object
        
            On Error GoTo ErrHandler
            
            Set TB = ActiveSheet.TextBoxes(ActiveSheet.TextBoxes.Count)
            
            With TB
                .Interior.Color = RGB(255, 255, 0)
                .Font.Name = "Arial"
                .Font.Size = 8
                .Font.Bold = True
                .Font.Color = RGB(0, 0, 255)
                .Text = .Name
                .AutoSize = False
            End With
            
    ErrHandler:
            If Err <> 0 Then
                Select Case Err.Number
                    Case 0:     Exit Sub
                    Case 1004:  Msg = "There are No Text Boxes on this Sheet."
                End Select
                    
                If Msg <> "" Then MsgBox Msg, vbExclamation
            End If
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-02-2007
    Posts
    2

    Unhappy Re: I can use VBA to create a text box, how do I set the background colour?

    Thanks for your help Leith, problem solved!

    I've been trying to incorporate that sort of code into my existing Macro but to no avail, and it's very frustrating - I'm trying to learn this but it didn't seem to be making much sense at the minute!
    Last edited by irf90; 09-30-2014 at 10:29 AM. Reason: Problem Solved

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    re: I can use VBA to create a text box, how do I set the background colour?

    Hello irf90,

    Trying changing
    Dim TB As Object
    To this
    Dim TB As Excel.TextBox

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 6
    Last Post: 07-31-2012, 08:41 AM
  2. Replies: 4
    Last Post: 12-21-2011, 11:32 AM
  3. [SOLVED] Change Row Background Colour When Text Is Entered Into A Cell
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2010, 01:28 PM
  4. formating text and background colour
    By pitway in forum Excel General
    Replies: 1
    Last Post: 09-14-2006, 02:42 PM
  5. [SOLVED] In Excel, colour formatting (text and background) reverts to blk
    By malcolm in forum Excel General
    Replies: 1
    Last Post: 07-12-2006, 03:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1