【问题标题】:Change cell format in a sheet according to Option Button in a form根据表单中的选项按钮更改工作表中的单元格格式
【发布时间】:2017-01-07 20:53:45
【问题描述】:

我有一个数据库和一个表单来输入数据。该表单正在将数据添加到表中的下一个空行。我的表单中有 4 个选项按钮,指示下一个条目是哪种类型的交易。我想根据选择的选项按钮格式化 B 列中单元格的背景,因此当我单击确认时,表单中的数据将插入数据库中,并且 B 列中单元格的背景颜色设置正确。我无法从该设备上传我的代码,但实际上背景颜色已设置但始终相同,并且如果我选择其他选项按钮也不会更改。

知道可能是什么问题吗?在应用选项按钮之前,我是否需要包含一行来清除以前的格式?

Private Sub CommandButton1_Click()
Dim L As Integer
Dim Code As String

If MsgBox("Confirm?", vbYesNo, "Confirming new invoice") = vbYes Then
    L = Sheets("FACTURE").Range("D65535").End(xlUp).Row + 1 'Pour placer le nouvel enregistrement _ la premi_re ligne de tableau non vide
    Range("C" & L).Value = (Now)
    Range("D" & L).Value = TextBox2
    Range("E" & L).Value = TextBox3
    Range("F" & L).Value = TextBox4
    Range("G" & L).Value = TextBox5
    Range("K" & L).Value = ComboBox1
    Range("L" & L).Value = ComboBox2
    Range("M" & L).Value = ComboBox3
    Range("N" & L).Value = TextBox9
    Range("O" & L).Value = TextBox10
    Range("R" & L).Value = TextBox39
    Range("P" & L).Value = TextBox40
End If

If OptionButton1.Enabled = True Then
Range("B" & L).Select
 With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent3
    .TintAndShade = 0.399975585192419
    .PatternTintAndShade = 0
 End With

 ElseIf OptionButton2.Enabled = True Then
 Range("B" & L).Select
  With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent1
    .TintAndShade = 0.399975585192419
    .PatternTintAndShade = 0
 End With

End If

End Sub

【问题讨论】:

  • 您使用的是 VBA 用户窗体还是工作表 ActiveX 的? 数据库究竟是什么?
  • 我使用 VBA 表单,数据库是工作表中的表
  • 请上传相关代码
  • 我在问题中添加了代码

标签: excel forms vba colors format


【解决方案1】:

你必须简单地使用:

If OptionButton1 Then

...

ElseIf OptionButton2 Then

...

End If

因为用户窗体控件的Enabled 属性“指定控件是否可以接收焦点并响应用户生成的事件。”

换句话说,它灰色变黑用户表单中的控件,使其相应地不可用或可用于用户输入

当您使用它时,`Value' 属性 “确定或指定是否选择了指定的选项按钮”

由于OptionButtonValue属性是默认的,你可以完全省略

此外,您可能需要考虑对代码进行以下小重构:

Option Explicit

Private Sub CommandButton1_Click()
    Dim L As Long
    Dim Code As String
    Dim TextBox2 As Long

    If MsgBox("Confirm?", vbYesNo, "Confirming new invoice") = vbYes Then
        With Worksheets("FACTURE")
            L = .Range(.Rows.Count, "D").End(xlUp).Row + 1 'Pour placer le nouvel enregistrement _ la premi_re ligne de tableau non vide
        End With
        With Me
            Range("C" & L).Value = (Now)
            Range("D" & L).Value = .TextBox2
            Range("E" & L).Value = .TextBox3
            Range("F" & L).Value = .TextBox4
            Range("G" & L).Value = .TextBox5
            Range("K" & L).Value = .ComboBox1
            Range("L" & L).Value = .ComboBox2
            Range("M" & L).Value = .ComboBox3
            Range("N" & L).Value = .TextBox9
            Range("O" & L).Value = .TextBox10
            Range("R" & L).Value = .TextBox39
            Range("P" & L).Value = .TextBox40

            If .OptionButton1.Enabled Then
               FormatCell Range("B" & L), xlThemeColorAccent3
            ElseIf .OptionButton2 Then
               FormatCell Range("B" & L), xlThemeColorAccent1
            End If
        End With
    End If
End Sub

Sub FormatCell(rng As Range, thColor As XlThemeColor)
    With rng.Interior
       .Pattern = xlSolid
       .PatternColorIndex = xlAutomatic
       .themeColor = thColor
       .TintAndShade = 0.399975585192419
       .PatternTintAndShade = 0
    End With
End Sub

地点:

  • 代码只有在用户确认后才会执行

  • 我使用更传统(和“最先进”)的模式来获取给定工作表列的第一个非空行

    L 类型的L 声明一起,使其独立于excel 版本控制,使其能够到达Excel 2003 后工作表行的底部

  • 我使用 With Me 块通过 dot (.) 表示法访问用户窗体控件

    这不仅有助于您对用户窗体属性和方法进行编码(在键入点 Intellisense 后会为您提供它们的列表),而且还可以避免它们可能出现的阴影。

    下面的代码应该解释你为什么:

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim TextBox2 As Long
    
        TextBox2 = 2
        Range("D3").Value = TextBox2 '<-- you're referring to 'TextBox2' Long variable value
        Range("D3").Value = Me.TextBox2 '<-- you're referring to the userform control named after "TextBox2"
    End Sub
    
  • 我分解出负责将单元格格式化为特定的代码 Function FormatCell()

    每次你看到一些重复的代码,那么是时候编写一个 Sub 或 Function 来使用适当的参数来处理它

    解耦不同的任务可以实现更可持续、更快速的代码调试和维护

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 2015-01-25
    • 2017-02-03
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多