在VBA中调用DblClick - 双击时关闭而不运行函数

时间:2016-07-21 18:20:58

标签: vba excel-vba excel

下午好,

忍受我,我是VBA的新手。我有一个Sub,当它双击时打开一个文本框,不仅我想要它打开文本框(展开它),我还希望它运行一个名为EMSpull的子。这一切都完美无缺,除了我想要双击以关闭文本框而不再运行EMSpull这一事实。

此外,有人可以解释"控制",特别是对于这种情况(我没有写出adjustheight)

代码低于

Private Sub txtEMS_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call AdjustHeight(txtEMS, lblEMS)
Call EMSpull
End Sub


Public Sub AdjustHeight(cControl As Control, cLabel As Control)
On Error GoTo errhand

If bExpandedMode = False Then
      dOldTop = cControl.Top
    dOldLeft = cControl.Left
    dOldWidth = cControl.Width
    dOldHeight = cControl.Height
    cControl.Top = lblDescription.Top + 50
    cControl.Width = cControl.Width * 2
    cControl.Height = 500
    cControl.Left = lblResults.Left
    bExpandedMode = True
    Call HideAllTxt(cControl)
    lblDescription.Visible = True
    lblDescription.Caption = cLabel.Caption
    If Len(cControl.Text) > 2 Then
      cControl.CurLine = 0
    End If
Else
    bExpandedMode = False
    Call ShowAllTxt
    lblDescription.Visible = False
    cControl.Top = dOldTop
    cControl.Left = dOldLeft
    cControl.Width = dOldWidth
    cControl.Height = dOldHeight
End If
Exit Sub
errhand:
Resume Next
End Sub

1 个答案:

答案 0 :(得分:1)

我假设你在潜艇的顶部有一个名为bExpandedMode的全局布尔值?如果是这样,这应该可以正常工作:

Private Sub txtEMS_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call AdjustHeight(txtEMS, lblEMS)
if bExpandedMode = true then Call EMSpull 'Calls it only when it expanded in AdjustHeight
End Sub


Public Sub AdjustHeight(cControl As Control, cLabel As Control)
On Error GoTo errhand

If bExpandedMode = False Then
      dOldTop = cControl.Top
    dOldLeft = cControl.Left
    dOldWidth = cControl.Width
    dOldHeight = cControl.Height
    cControl.Top = lblDescription.Top + 50
    cControl.Width = cControl.Width * 2
    cControl.Height = 500
    cControl.Left = lblResults.Left
    bExpandedMode = True
    Call HideAllTxt(cControl)
    lblDescription.Visible = True
    lblDescription.Caption = cLabel.Caption
    If Len(cControl.Text) > 2 Then
      cControl.CurLine = 0
    End If
Else
    bExpandedMode = False
    Call ShowAllTxt
    lblDescription.Visible = False
    cControl.Top = dOldTop
    cControl.Left = dOldLeft
    cControl.Width = dOldWidth
    cControl.Height = dOldHeight
End If
Exit Sub
errhand:
Resume Next
End Sub

基本上如果布尔存在并且像我想的那样使用它,它只是检查盒子是否现在扩展。如果不是,则布尔值为False,AdjustHeight将其展开,然后将其变为true。相反,当它设置为True时,它会关闭它,并将其设置为False。

所以我的修复只是检查相同的布尔值并且只运行它1路(当它刚刚展开时)