运行代码后Excel菜单栏冻结。点击鼠标后取消冻结

时间:2016-11-25 15:35:27

标签: excel vba excel-vba

我正在使用带有按钮的表单从Intranet下载工作表,并使用源工作表中的数据填充此目标工作表。这部分有效:数据填充,表格卸载,脚本停止,我的目标表格作为活动屏幕。
现在出现了问题:我可以选择单元格,但不能使用Excel中的任何功能,不能在快速访问功能区上按任何按钮,如文件,主页或保存。推ESC没有用。唯一解冻Excel的是在随机单元格的某处按下鼠标右键。它'解锁'excel,我可以去文件菜单。我不知道在哪里看。我认为它有点小,但无法弄清楚它是什么。

这是代码(跳转用于单项或多项。如果是1项,我跳过循环。也许有更好的选择吗?):

Private Sub genbutton_Click()
    On Error GoTo 0

    Dim startsheet As String
    startsheet = ActiveSheet.Name
    roww = ActiveCell.Row

    Set Source = ActiveWorkbook.ActiveSheet

    'search for column in source sheet
    kolommaint = kolomnaam2("Maintenance Plan")
    kolomfloc = kolomnaam2("Functional Location")
    kolomdescrip = kolomnaam2("Maintenance item description")
    kolomequip = kolomnaam2("Equipment")

    'find last row on data source page
    With ActiveSheet
        lastrow = .Cells(.Rows.Count, kolommaint).End(xlUp).Row
    End With

    Set destinationsheet = Workbooks.Open("http:// sheet on intranet.xlsm")

    'find first data row on destination sheet
    Dim FindString As String
    Dim Rng As Range
    FindString = "Action"
    With destinationsheet.Sheets("Data input").Range("A:A")
        Set Rng = .Find(What:=FindString, _
                    After:=.Cells(.Cells.Count), _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
        If Not Rng Is Nothing Then
            Application.Goto Rng, True
            datarij = ActiveCell.Row + 1
        End If
    End With
    'make a jump to avoid loop if only 1 item is needed
    If callsingle.Value = True Then
        i = roww

        If Source.Range(kolommaint & i).Value = "" Then
            GoTo verdergaan
        End If
        GoTo jump
    End If

    For i = eerstedatarij To lastrow

jump:
        'skip row if empty
        If Source.Rows(i).Hidden = True Then
            GoTo verdergaan
        End If

        destinationsheet.Sheets("Data input").Range("A" & datarij).Value = "Release Call"
        destinationsheet.Sheets("Data input").Range("B" & datarij).Value = Source.Range(kolommaint & i).Value
        destinationsheet.Sheets("Data input").Range("C" & datarij).Value = "PM"
        destinationsheet.Sheets("Data input").Range("E" & datarij).Value = Source.Range(kolomdescrip & i).Value
        destinationsheet.Sheets("Data input").Range("F" & datarij).Value = Source.Range(kolomdescrip & i).Value
        destinationsheet.Sheets("Data input").Range("G" & datarij).Value = Source.Range(kolomfloc & i).Value
        destinationsheet.Sheets("Data input").Range("H" & datarij).Value = Source.Range(kolomequip & i).Value

        datarij = datarij + 1

        'make jump if single item is used
        If callsingle.Value = True Then
            GoTo jump2
        End If

verdergaan:
    Next i
jump2:

    destinationsheet.Sheets("Data input").Range("A13").Select

    Set Source = Nothing
    Set destinationsheet= Nothing

    Unload Me

End Sub

我现在正在使用以下代码解冻excel:

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
 Const MOUSEEVENTF_RIGHTDOWN = &H8
 Const MOUSEEVENTF_RIGHTUP = &H10

 Public Sub RightDown()
     mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
     mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
 End Sub

1 个答案:

答案 0 :(得分:0)

我在冻结Excel函数时遇到了类似的问题。我相信它是由使用“Worksheets.Open”

打开工作表时的焦点错误引起的

执行“Worksheets.Open”时显示用户窗体时出现问题。我必须先隐藏用户窗体,然后打开工作表以避免冻结问题。

导致冻结excel函数的代码:

Private Sub btnBrowse_Click()
  Dim retVal As Integer
  Dim fDialog As FileDialog
  Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

  fDialog.AllowMultiSelect = False
  retVal = fDialog.Show()

  If retVal = -1 Then
    Application.Workbooks.Open fDialog.SelectedItems(1)
    Me.Hide   ' Hiding my userform
  End If
End Sub

但我找到了一个使用hide冻结的解决方案并显示工作表窗口。

  Dim aWorkbook As Workbook
  Set aWorkbook = ActiveWorkbook
  aWorkbook.Windows(1).Visible = False
  aWorkbook.Windows(1).Visible = True

然后它奏效了。唯一的缺点是更改可见性会使工作表处于更改状态。如果可能的话,最好只是在打开工作簿之前隐藏userform。