Visual Basic应用程序

时间:2015-05-31 18:55:23

标签: excel vba

如果有人可以帮助我,我会很高兴... 我在VBA中编写了一个代码可以计算几个东西的代码,这段代码必须用一种颜色突出显示excell中的一些单元格,但他没有这样做,我也不知道为什么......

以下是代码:

Private Sub CommandButton1_Click()
    Dim studenti As Integer
    Dim prezenti As Integer
    Dim absenti As Integer
    Dim i As Integer

    studenti = ActiveSheet.Columns("D").SpecialCells(xlTextValues).Rows.Count - 1

    prezenti = 0
    absenti = 0
    For i = 3 To studenti + 2
        If (Cells(i, 4).Value = "a") Then
            absenti = absenti + 1
            If (OptionButton2.Value = True) Then
                Cells(i, 3).Font.Color = vbGreen 'RGB(255, 0, 0)
            End If
        End If

        If (Cells(i, 4).Value = "p") Then
            prezenti = prezenti + 1
            If (OptionButton1.Value = True) Then
                Cells(i, 3).Font.Color = RGB(0, 255, 0)
            End If
        End If

        If (OptionButton3.Value = True) Then
            Cells(i, 3).Font.Color = RGB(0, 0, 0)
        End If
    Next i

1 个答案:

答案 0 :(得分:0)

以下Excel VBA代码段将执行核心操作,即:使用红色/绿色文本颜色突出显示absenti / presenti(D列中的文本值“a”或“p”):

Private Sub CommandButton1_Click()
    Dim studenti As Integer
    Dim prezenti As Integer
    Dim absenti As Integer
    Dim i As Integer

    studenti = Cells(Rows.Count, "D").End(xlUp).Row
    For i = 3 To studenti + 2
        If (Cells(i, 4).Value = "a") Then
            Cells(i, 4).Font.Color = RGB(255, 0, 0) 'RED
        ElseIf (Cells(i, 4).Value = "p") Then
            Cells(i, 4).Font.Color = RGB(0, 255, 0) 'GREEN
        End If
    Next i
End Sub

您可以进一步修改它以添加业务逻辑(所有OptionButton.Value,计数器等)。

希望这会有所帮助。最好的问候