通过对话框打开文件后代码不连续

时间:2018-09-21 06:10:40

标签: excel vba excel-vba

我现在很困惑...我有两个模块open_filesstart_comparison。我从start_comparison呼叫open_files,它应该打开文件打开对话框。然后,应假定用户选择一个文件并单击打开。通过start_comparison,用户应该打开两个文件。但是有时(这让我感到困惑),代码打开了第一个文件,但随后偶尔会退出start_comparison。有时它起作用,有时不起作用,我不知道何时何地。下面是代码。

我的想法是:显示文件对话框时,可以双击该文件,该文件将以openend开头,这将触发隐藏的退出。但是,我无法证实这一假设。当我逐步执行时,一切正常。

您对这个问题有什么看法?

Sub start_comparison()

Dim cell As Range
Dim control_file_storage_bins As Range
Dim last_row_CONTROLFILE As Long

Application.ScreenUpdating = False

Set ws_control_file = ActiveWorkbook.ActiveSheet

Range("A2:Z1048576").Clear

Call open_files("PHYSICAL STOCK", 1)
Call open_files("STORAGE BINS", 2)

'Copy stock information

With ws_control_file

.Range("A2:A" & last_row_PHYSICALSTOCK).Value = ws_physical_stock.Range("B2:B" & last_row_PHYSICALSTOCK).Value
.Range("B2:B" & last_row_PHYSICALSTOCK).Value = ws_physical_stock.Range("C2:C" & last_row_PHYSICALSTOCK).Value
.Range("C2:C" & last_row_PHYSICALSTOCK).Value = ws_physical_stock.Range("J2:J" & last_row_PHYSICALSTOCK).Value
.Range("D2:D" & last_row_PHYSICALSTOCK).Value = ws_physical_stock.Range("K2:K" & last_row_PHYSICALSTOCK).Value
.Range("E2:E" & last_row_PHYSICALSTOCK).Value = ws_physical_stock.Range("E2:E" & last_row_PHYSICALSTOCK).Value

End With

Set control_file_storage_bins = ws_control_file.Range("A2:A" & last_row_PHYSICALSTOCK)

For Each cell In rng_STORAGEBIN

If (WorksheetFunction.CountIf(control_file_storage_bins, cell.Value) = 0) Then 'Storage Bin empty

    With ws_control_file

        last_row_CONTROLFILE = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        .Cells(last_row_CONTROLFILE, "A").Value = cell.Value
        .Range("B" & last_row_CONTROLFILE & ":E" & last_row_CONTROLFILE).Value = "BIN EMPTY"

    End With

End If

Next cell

wb_physical_stock.Close (False)
wb_storage_bins.Close (False)

Application.ScreenUpdating = True

MsgBox "Success!"

End Sub

其他过程:

Sub open_files(file_type As String, wb_object As Integer)

Dim last_row_STORAGEBIN As Long

MsgBox "Please select the relevant " & file_type & " extract!"

With Application.FileDialog(msoFileDialogOpen)

    .AllowMultiSelect = False
    .Show

    Workbooks.Open (.SelectedItems(1))

    Select Case wb_object

        Case 1 'Physical Stock

            Set wb_physical_stock = ActiveWorkbook

            With wb_physical_stock

                Set ws_physical_stock = ActiveSheet

                last_row_PHYSICALSTOCK = ws_physical_stock.Cells(Rows.Count, "A").End(xlUp).Row

            End With

        Case 2 'Storage Bins

            Set wb_storage_bins = ActiveWorkbook

            With wb_storage_bins

                Set ws_storage_bins = ActiveSheet

                last_row_STORAGEBIN = ws_storage_bins.Cells(Rows.Count, "A").End(xlUp).Row - 1

                Set rng_STORAGEBIN = ws_storage_bins.Range("A2:A" & last_row_STORAGEBIN)

            End With

    End Select

End With

End Sub

以防万一,这是私有变量声明:

Private wb_physical_stock As Workbook, wb_storage_bins As Workbook
Private ws_physical_stock As Worksheet, ws_storage_bins As Worksheet, ws_control_file As Worksheet
Private last_row_PHYSICALSTOCK As Long
Private rng_STORAGEBIN As Range

编辑:我现在正在检查带有断点的过程open_files。如果我在Workbooks.Open之前设置了一个断点,然后使用F5从那里再次运行,一切都很好。但是,如果我在Workbooks.Open之后设置一个断点,则甚至不会触发该断点。有什么想法吗?

EDIT2:以前,宏是通过快捷方式启动的。现在,我将其更改为ActiveX-Control,它可以正常工作。使用简单的表单和按钮(表单控件)进行了相同的测试。

1 个答案:

答案 0 :(得分:0)

如果您怀疑打开文件会触发一些代码,请在打开文件之前先禁用事件-这样可以防止对该文件执行任何(autoexec-)宏。

您应该解决的另一个主题是用户可能按下“取消”按钮,否则您将遇到运行时错误。您可以使用show方法的结果进行检查,如果取消了对话框,它将返回False

With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    if .Show then
        application.EnableEvents = False
        Workbooks.Open (.SelectedItems(1))
        application.EnableEvents = True
        (...)
    else
        ' You have to make up your mind what to do in that case...
    end if
end with