简体   繁体   English

Excel VBA。 将 TextBox 的 BackColor 与单元格的 .Interior.Color 匹配

[英]Excel VBA. Matching the BackColor of a TextBox to the .Interior.Color of a cell

I can't seem to find an answer for this.我似乎无法找到答案。 I have a spreadsheet with a userform, and I'm trying to match the back colour of TextBox47 with the corresponding cell on the sheet, the value of which is fetched via a Listbox.我有一个带有用户表单的电子表格,我试图将 TextBox47 的背景颜色与工作表上的相应单元格匹配,其值是通过列表框获取的。 What I need is to be able to click on the list item, and the text box to fill with that colour.我需要的是能够点击列表项,并用该颜色填充文本框。

I have the following code to colour the cells in the sheet, according to date, and with named ranges for parameters.. and this runs on UserForm Initialise()我有以下代码根据日期和参数的命名范围为工作表中的单元格着色..这在 UserForm Initialise() 上运行

Dim cell As Range
      With Range("data_table[Date Test]")
    
     
    For Each cell In Range("data_table[Date Test]")
        If cell.Value < Range("Today") Then
            cell.Interior.ColorIndex = 6
            ElseIf cell.Value >= Range("Today") And cell.Value <= Range("Thirty_Days") Then
            cell.Interior.ColorIndex = 3
            ElseIf cell.Value > Range("Thirty_Days") And cell.Value <= Range("Sixty_Days") Then
            cell.Interior.ColorIndex = 45
            ElseIf cell.Value > Range("Sixty_Days") And cell.Value <= Range("Ninety_Days") Then
            cell.Interior.ColorIndex = 43
            ElseIf cell.Value > Range("Ninety_Days") Then
            cell.Interior.ColorIndex = 18

         End If

    Next cell
    End With

This works fine, I then tried the following to colour the textbox这很好用,然后我尝试了以下方法来为文本框着色

 Private Sub TextBox47_Change()
    Me.TextBox47.Text = Format(TextBox47.Text, "dd/mmm/yyyy")
    Dim cell As Range
For Each cell In Range("data_table[Date Test]")
          With Range("data_table[Date Test]")
    Me.TextBox47.BackColor = .Interior.Color
        Next cell
    
    End With

I think I need to add the following somewhere..我想我需要在某处添加以下内容..

TextBox47.Value = Me.ListBox1.List(ListBox1.ListIndex, 65)

Which is the ListBox reference for the TextBox.. but I'm starting to get very confused now.这是 TextBox 的 ListBox 参考。但我现在开始变得非常困惑。 I've been looking at this for a couple of days.我一直在看这个几天。


Edit to add Listbox code as asked.编辑以按要求添加列表框代码。

    Private Sub ComboBox8_Change()

Dim i As Long

Me.ListBox1.Clear
For i = 2 To Application.WorksheetFunction.CountA(Sheet1.Range("A:A"))
If Sheet1.Cells(i, 10).Value = Me.ComboBox8.Value Then

Me.ListBox1.AddItem Sheet1.Cells(i, 10).Value
'ID Number
Me.ListBox1.List(ListBox1.ListCount - 1, 0) = Sheet1.Cells(i, 1).Value
'Title
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(i, 2).Value

So would it be more something like this..所以会不会更像这样..

 Private Sub TextBox47_Change()
    Me.TextBox47.Text = Format(TextBox47.Text, "dd/mmm/yyyy")
Me.TextBox47.BackColor = .Interior.Color
    End Sub

Which gives a Compile Error这给出了编译错误

在此处输入图片说明1 1

Newer code较新的代码

I've now changed it to this..我现在已经改成这样了。。

Private Sub TextBox47_Change()
    Dim cell As Range
        Me.TextBox47.Text = Format(TextBox47.Text, "dd/mmm/yyyy")
            With Range("data_table[Date Test]")
                Me.TextBox47.BackColor = Range("data_table[Date Test]").Interior.Color
            End With
End Sub

Which at least doesn't throw up an error, but on every selection the BackColor is black.这至少不会引发错误,但在每次选择时,BackColor 都是黑色的。 I started with ..我从..

Private Sub TextBox47_Change()
        Dim cell As Range
            Me.TextBox47.Text = Format(TextBox47.Text, "dd/mmm/yyyy")
                With Range("data_table[Date Test]")
                    Me.TextBox47.BackColor = Range("cell").Interior.Color
                End With
    End Sub

But that just stopped at a Run Time error 1004但这只是在运行时错误 1004 处停止

Yet another edit.. please tell me if continual adding to this post is the wrong way to do it.另一个编辑..请告诉我继续添加到这篇文章是否是错误的方法。

在此处输入图片说明

This, when hovering over is actually picking up the correct info from the cell that I'm after (I've tried different ones to make sure), in the Listbox selection that I'm clicking on, so I guess now it's a case of telling the Interior.Colour to look at the cell that is chosen?这个,当悬停在我点击的列表框选择中时,实际上是从我所追求的单元格中获取正确的信息(我尝试了不同的信息来确保),所以我想现在是一个案例告诉 Interior.Colour 查看选择的单元格?

I managed to get it to work.. although not sure if it's the most elegant solution.我设法让它工作......虽然不确定它是否是最优雅的解决方案。 :) :)

To save confusing it with the code above, which has gotten a bit disjointed and messy, here's all the new working code and the explanation I came up with为了避免与上面的代码混淆,上面的代码有点脱节和混乱,这里是所有新的工作代码和我想出的解释

Code to colour cells in sheet:为工作表中的单元格着色的代码:

Dim cell As Range
  
      For Each cell In Range("data_table[Date Reviewed]")
        If cell.Value < Range("Today") Then
            cell.Interior.ColorIndex = 7  'Magenta
            ElseIf cell.Value >= Range("Today") And cell.Value <= Range("Thirty_Days") Then
            cell.Interior.ColorIndex = 3  'Red
            ElseIf cell.Value > Range("Thirty_Days") And cell.Value <= Range("Sixty_Days") Then
            cell.Interior.ColorIndex = 45  'Orange
            ElseIf cell.Value > Range("Sixty_Days") And cell.Value <= Range("Ninety_Days") Then
            cell.Interior.ColorIndex = 4  'Green
            ElseIf cell.Value > Range("Ninety_Days") Then
            cell.Interior.ColorIndex = 19  'Beige
        End If

    Next cell

This is in UserForm_Initialize()这是在UserForm_Initialize()

And then to match the cell colour with TextBox.BackColor , I've used the ListBox.ListIndex to get the date, using the following code:然后将单元格颜色与TextBox.BackColor匹配,我使用ListBox.ListIndex获取日期,使用以下代码:

Private Sub TextBox47_Change()

      Me.TextBox47.Text = Format(TextBox47.Text, "dd/mm/yyyy")
      
                    If (Me.ListBox1.List(ListBox1.ListIndex, 65)) < Range("Today") Then
                     TextBox47.BackColor = RGB(255, 0, 255)  'Magenta
                ElseIf (Me.ListBox1.List(ListBox1.ListIndex, 65)) >= Range("Today") And (Me.ListBox1.List(ListBox1.ListIndex, 65)) <= Range("Thirty_Days") Then
                     TextBox47.BackColor = RGB(255, 0, 0)  'Red
                ElseIf (Me.ListBox1.List(ListBox1.ListIndex, 65)) > Range("Thirty_Days") And (Me.ListBox1.List(ListBox1.ListIndex, 65)) <= Range("Sixty_Days") Then
                     TextBox47.BackColor = RGB(255, 153, 0)  'Orange
                ElseIf (Me.ListBox1.List(ListBox1.ListIndex, 65)) > Range("Sixty_Days") And (Me.ListBox1.List(ListBox1.ListIndex, 65)) <= Range("Ninety_Days") Then
                     TextBox47.BackColor = RGB(0, 255, 0)  'Green
                ElseIf (Me.ListBox1.List(ListBox1.ListIndex, 65)) > Range("Ninety_Days") Then
                     TextBox47.BackColor = RGB(255, 255, 204)  'Beige
        End If
        
     End Sub

I can now call that TextBox.BackColor to set the associated Interior.Color of any other cell I'm forwarding on that value to, such as the Report I'm generating in Sheet2:我现在可以调用该TextBox.BackColor来设置我将该值转发到的任何其他单元格的关联Interior.Color ,例如我在 Sheet2 中生成的报告:

Sheet2.Range("D105").Value = Me.TextBox47.Text
Sheet2.Range("D105").Interior.Color = Me.TextBox47.BackColor

Thankyou for your help and patience.感谢您的帮助和耐心。 It was your comment "So you have a listbox which returns a date" which helped me to see where I was actually getting the date from when hovering over the line of code, rather than it seeing a particular Cell.Address , and hence how I got the above to work.是你的评论“所以你有一个返回日期的列表框”,它帮助我看到当Cell.Address悬停在代码行上时我实际上是从哪里获取日期的,而不是它看到特定的Cell.Address ,因此我如何得到了上述工作。

Again, thankyou, and I hope I didn't break too many posting rules in the meantime.再次,谢谢你,我希望在此期间我没有违反太多的发帖规则。 :) :)

Apologies, but I couldn't see a thanks button anywhere.抱歉,但我在任何地方都看不到感谢按钮。

Cheers干杯

Liam利亚姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM