繁体   English   中英

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

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

我似乎无法找到答案。 我有一个带有用户表单的电子表格,我试图将 TextBox47 的背景颜色与工作表上的相应单元格匹配,其值是通过列表框获取的。 我需要的是能够点击列表项,并用该颜色填充文本框。

我有以下代码根据日期和参数的命名范围为工作表中的单元格着色..这在 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

这很好用,然后我尝试了以下方法来为文本框着色

 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

我想我需要在某处添加以下内容..

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

这是 TextBox 的 ListBox 参考。但我现在开始变得非常困惑。 我一直在看这个几天。


编辑以按要求添加列表框代码。

    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

所以会不会更像这样..

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

这给出了编译错误

在此处输入图片说明 1

较新的代码

我现在已经改成这样了。。

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

这至少不会引发错误,但在每次选择时,BackColor 都是黑色的。 我从..

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

但这只是在运行时错误 1004 处停止

另一个编辑..请告诉我继续添加到这篇文章是否是错误的方法。

在此处输入图片说明

这个,当悬停在我点击的列表框选择中时,实际上是从我所追求的单元格中获取正确的信息(我尝试了不同的信息来确保),所以我想现在是一个案例告诉 Interior.Colour 查看选择的单元格?

我设法让它工作......虽然不确定它是否是最优雅的解决方案。 :)

为了避免与上面的代码混淆,上面的代码有点脱节和混乱,这里是所有新的工作代码和我想出的解释

为工作表中的单元格着色的代码:

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

这是在UserForm_Initialize()

然后将单元格颜色与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

我现在可以调用该TextBox.BackColor来设置我将该值转发到的任何其他单元格的关联Interior.Color ,例如我在 Sheet2 中生成的报告:

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

感谢您的帮助和耐心。 是你的评论“所以你有一个返回日期的列表框”,它帮助我看到当Cell.Address悬停在代码行上时我实际上是从哪里获取日期的,而不是它看到特定的Cell.Address ,因此我如何得到了上述工作。

再次,谢谢你,我希望在此期间我没有违反太多的发帖规则。 :)

抱歉,但我在任何地方都看不到感谢按钮。

干杯

利亚姆

暂无
暂无

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

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