【问题标题】:excel VBA how to make a floating button?excel VBA如何制作浮动按钮?
【发布时间】: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的表单。
  • 冻结窗格更容易

标签: excel button floating vba


【解决方案1】:

这里是:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    On Error GoTo 0
    With Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn)
        CommandButton1.Top = .Top + 100
        CommandButton1.Left = .Left + 300
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2019-08-05
    • 2020-05-27
    • 2021-06-20
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多