ActiveX Command Button that unhides next to a Cell

2019-07-17 07:20发布

I have 80 rows where the user can enter a predetermined value under column Ward. This unhides a button next to it. Upon clicking it, it empties the adjacent value and increments (+1) a particular cell in another sheet depending on the original value.

Currently, I have 80 ActiveX buttons next to the Ward cells that hides/unhides depending on the value of the Ward cells. I've noticed that adding more buttons slows down the spreadsheet because of the sheer volume of If Then statements I have.

If Range("F8").Value = 0 Then
  Sheets("Admissions").EDAdmit1.Visible = False
Else
  Sheets("Admissions").EDAdmit1.Visible = True
End If

If Range("L8").Value = 0 Then
  Sheets("Admissions").ElecAdmit1.Visible = False
Else
  Sheets("Admissions").ElecAdmit1.Visible = True
End If

If Range("F9").Value = 0 Then
  Sheets("Admissions").EDAdmit2.Visible = False
Else
  Sheets("Admissions").EDAdmit2.Visible = True
End If

If Range("L9").Value = 0 Then
  Sheets("Admissions").ElecAdmit2.Visible = False
Else
  Sheets("Admissions").ElecAdmit2.Visible = True
End If

.. and so on.

Not to mention the If Then statements I have for every button click.

Private Sub EDAdmit1_Click()
If Range("F8") = "ICU" Then
    Worksheets("Overview").Range("AD11").Value = Worksheets("Overview").Range("AD11") + 1
ElseIf Range("F8") = "HDU" Then
    Worksheets("Overview").Range("AF11").Value = Worksheets("Overview").Range("AF11") + 1
ElseIf Range("F8") = "DPU" Or Range("F8") = "Other" Then
Else
    Col = WorksheetFunction.VLookup(Range("F8"), Range("U1:V27"), 2)
    Worksheets("Overview").Range(Col).Value = Worksheets("Overview").Range(Col).Value + 1
End If
Range("F8").ClearContents
End Sub

Is there a more efficient way of doing this?

Admission List:

Admission List

Tranfers

标签: excel vba
2条回答
\"骚年 ilove
2楼-- · 2019-07-17 08:01

At the beginning of your code put this line:

Application.ScreenUpdating = False

this will disable all screen updates. Let your code do changes, and then enable screen updating, and all your changes will appear.

Application.ScreenUpdating = True

Disabling screen updating usually makes the execution of code faster.

查看更多
【Aperson】
3楼-- · 2019-07-17 08:17

You could consider using "admit" hyperlinks in the cells next to the Ward selections: that way you only need one handler (Worksheet_FollowHyperlink in the worksheet module). Note you need to use Insert >> Hyperlink and not the HYPERLINK() formula-type links here (because formula-based links don't trigger the FollowHyperlink event).

You can ditch the hide/show code and instead use conditional formatting to change the link font color to hide the links when there's no Ward selected. If a user clicks on one of the hidden links then you can just do nothing.

enter image description here

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim rngSrc As Range, addr, ward

    Set rngSrc = Target.Range        '<< the cell with the link
    ward = rngSrc.Offset(0, 1).Value '<< cell with Ward

    'only do anything if a ward is selected
    If Len(ward) > 0 Then
        'find the cell to update
        Select Case ward
            Case "ICU"
                addr = "AD11"
            Case "HDU"
                addr = "AF11"
            Case "DPU", "Other"
                addr = ""
            Case Else
                addr = Application.VLookup(ward, Me.Range("U1:V27"), 2, False)
        End Select

        'if we have a cell to update then
        If Len(addr) > 0 Then
            With Worksheets("Overview").Range(addr)
                .Value = .Value + 1
            End With
        End If
        rngSrc.Offset(0, 1).ClearContents
    End If

    rngSrc.Select '<< select the clicked-on link cell
                  '   (in case the link points elsewhere)

End Sub
查看更多
登录 后发表回答