Consulting

Results 1 to 12 of 12

Thread: Solved: Color on userform

  1. #1

    Solved: Color on userform

    Hello all, I was wondering if it's possible to show colors of cells on a userform.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Colour of cells or the colour of objects on the user form?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Private Sub UserForm_Initialize()
    Worksheets("Sheet1").Range("A1").Interior.Color = vbRed
    Worksheets("Sheet1").Range("B1").Interior.Color = vbBlue
    End Sub

    Private Sub CommandButton1_Click()
    UserForm1.BackColor = Worksheets("Sheet1").Range("A1").Interior.Color
    End Sub

    Private Sub CommandButton2_Click()
    UserForm1.BackColor = Worksheets("Sheet1").Range("B1").Interior.Color
    End Sub

    Private Sub CommandButton3_Click()
    Unload Me
    End Sub[/VBA]

  4. #4
    Thank you for you responses and so sorry for my late reponse.

    I need help for something like this,..

    range A1 contains a certain data and yellow for interior-color, I would like to use listbox to display range A1 (data + color) on userform

    Thanks,

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you are showing one cell, a TextBox would be better than a list box.
    All the items in a list box will have the same format (font size, font color, back color, etc.)

    What code do you have so far for filling the control?

    Also, how does A1 get its color. Detecting color that is the result of Conditional Formatting, is different than detecting the color set by the user.

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]
    Private Sub UserForm_Initialize()
    With ListBox1
    .RowSource = "Sheet1!A1"
    .BackColor = Worksheets("Sheet1").Range("A1").Interior.Color
    End With
    End Sub[/VBA]

  7. #7
    @ mikerickson and Kenneth Hobs for your kind helps.


    I just knew that "All the items in a list box will have the same format (font size, font color, back color, etc.)". What I wanted to do (attached) might not be possible.

    Thanks again,


    http://img837.imageshack.us/img837/2...01290814am.jpg


  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I understand that there is something called a SpreadSheet control that isn't available on my Mac that might do what you wanted.

    If I had to do that, I'd use a bunch of text boxes.

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You make a code module, called clsLinkedTextBox, with this code.

    [VBA]' in code module clsLinkedTextBox

    Public WithEvents TextBox As MSForms.TextBox

    Property Get LinkedCell() As Range
    On Error Resume Next
    Set LinkedCell = Range(TextBox.Tag)
    On Error GoTo 0
    End Property

    Property Set LinkedCell(aCell As Range)
    With TextBox
    .Tag = aCell.Address(, , , True)
    .AutoWordSelect = False
    .BackColor = aCell.Interior.Color
    .BorderColor = aCell.Borders.Color
    If .BorderColor = 0 Then .BorderColor = 12566463
    .BorderStyle = fmBorderStyleSingle
    With .Font
    .Bold = aCell.Font.Bold
    .Italic = aCell.Font.Italic
    .Name = aCell.Font.Name
    .Size = aCell.Font.Size
    .StrikeThrough = aCell.Font.StrikeThrough
    .Underline = (aCell.Font.Underline <> xlUnderlineStyleNone)
    End With
    .ForeColor = aCell.Font.Color
    .IntegralHeight = True
    .MultiLine = aCell.WrapText
    .SelectionMargin = False
    .SpecialEffect = fmSpecialEffectFlat

    Select Case aCell.HorizontalAlignment
    Case xlGeneral
    If TypeName(aCell.Value) = "Double" Then
    .TextAlign = fmTextAlignRight
    Else
    .TextAlign = fmTextAlignLeft
    End If
    Case xlRight
    .TextAlign = fmTextAlignRight
    Case xlLeft
    .TextAlign = fmTextAlignLeft
    Case xlCenter
    .TextAlign = fmTextAlignCenter
    End Select

    .Text = aCell.Text
    End With
    End Property[/VBA]

    And put this code in the userform.


    [VBA]' in userform code module

    Private Sub CommandButton2_Click()
    Call LayOutRange(Sheet1.Range("a1:b2"), 5, 5):Rem adjust
    End Sub

    Sub LayOutRange(dupRange As Range, ufLeft As Single, ufTop As Single)
    Dim oneCell As Range, NewText As clsLinkedTextBox
    Dim cellOffsetLeft As Single, cellOffsetTop As Single
    Set NewTextBoxes = New Collection

    cellOffsetLeft = dupRange.Left
    cellOffsetTop = dupRange.Top

    With UserForm1
    For Each oneCell In dupRange
    Set NewText = New clsLinkedTextBox
    With NewText
    Set .TextBox = Me.Controls.Add("Forms.TextBox.1")
    With .TextBox
    .Left = ufLeft + oneCell.Left
    .Top = ufTop + oneCell.Top
    .Height = oneCell.Height
    .Width = oneCell.Width
    End With
    Set .LinkedCell = oneCell
    End With
    NewTextBoxes.Add Item:=NewText, Key:=oneCell.Address(, , , True)
    Next oneCell
    End With
    End Sub[/VBA]

  10. #10
    I am sorry. I am quite new for excel. Could you please give me an example file or explain a little more about the first part of the code (clsLinkedTextBox)? After I created a code module, called clsLinkedTextBox then showed the userform, an error occured.

    http://img41.imageshack.us/img41/828...12120245pm.jpg

  11. #11
    @ mikerickson

    Thank you very much. It's now working very nicely. I created a normal module intead of class module so that's why it did not work.

  12. #12
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I'm glad it helped.

    I should mention that the reason for the class module is that if you want to have the created textboxes respond to events, the event code will go in the class module.
    Note that the .Tag property of the created textbox is the address of its linked cell.
    Last edited by mikerickson; 04-01-2012 at 09:08 AM.

Posting Permissions

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