【发布时间】:2017-11-01 08:20:43
【问题描述】:
有一个代码可以检查日期并用适当的颜色填充单元格,并且该代码包含一个按钮,当用户单击该按钮时,它将运行 VBA 代码。
无论用户如何向下滚动,我都希望始终使按钮可见。
代码:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Cells(i, 3).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Debug.Print Len(Range("F" & i).Value)
Next
End Sub
我将不胜感激。
【问题讨论】:
-
这是什么按钮?表单控件还是 ActiveX?
-
@Robin Mackenzie 表单控件
-
三种可能的解决方案:(1) 将按钮放在功能区/菜单中 (2) 将按钮放在顶行并固定该行(视图 --> 冻结窗格)或 (3) 将表单上的按钮并显示模态为
False的表单。 -
冻结窗格更容易