【问题标题】:VBA Use variable value across modulesVBA 跨模块使用变量值
【发布时间】:2017-06-08 20:20:42
【问题描述】:

我正在尝试跨其他私有模块使用我在模块 1 中设置的工作簿的名称,但根据我的设置方式,我会遇到不同的错误。我在代码中添加了 cmets 来解释不同场景下会发生什么。

Option Explicit

Sub TestSharedVars()

CopyCellsthenClose
OpenNewWksheet (AlphaExportBook)

' *** Like this
' OpenNewWksheet (AlphaExportBook) I get "Error Variable not defined"

' *** Like this
' OpenNewWksheet I get "Error Argument not optional"

CloseWkbook


End Sub

Private Sub CopyCellsthenClose()
Dim AlphaExportBook As Workbook
Dim theRows
Dim theColumns
    With ActiveSheet.UsedRange
        theRows = .Rows.Count
        theColumns = .Columns.Count
        Range(Cells(1, 1), Cells(theRows, theColumns)).Select
    End With
        Selection.Copy

    Set AlphaExportBook = ActiveWorkbook


End Sub


Private Sub OpenNewWksheet()

'******************************
'    Open the File Dialog
'******************************
Dim ReversionWBook As Workbook


    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Show
        .Execute
  If (.SelectedItems.Count = 0) Then
        MsgBox "User Cancelled Operation"
'        GoTo EndofInstructions
    Else
    End If
    End With
    ActiveWorkbook.Activate
    Set ReversionWBook = ActiveWorkbook
End Sub

Private Sub CloseWkbook(AlphaExportBook As Workbook)

'**********************************
'  Close Alpha Export WorkBook
'**********************************
    AlphaExportBook.Activate
    Application.DisplayAlerts = False
    AlphaExportBook.Close SaveChanges:=False
    Application.DisplayAlerts = True

End Sub 

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    首先,调用OpenNewWksheet 时不应出现“Argument not optional”错误,因为该子例程不需要参数。尝试在不指定参数的情况下调用 CloseWkbook 时会出现该错误,因为该子例程需要将 Workbook 对象传递给它。


    使工作簿可用于所有子例程的最简单方法是使用模块级范围声明变量,例如

    Option Explicit
    Dim AlphaExportBook As Workbook
    
    Sub TestSharedVars()
        CopyCellsthenClose
        OpenNewWksheet
        CloseWkbook
    End Sub
    
    Private Sub CopyCellsthenClose()
        Dim theRows
        Dim theColumns
        With ActiveSheet.UsedRange
            theRows = .Rows.Count
            theColumns = .Columns.Count
            'Note - the following line won't do what you expect unless
            '       UsedRange starts at cell A1
            Range(Cells(1, 1), Cells(theRows, theColumns)).Select
        End With
        Selection.Copy
    
        Set AlphaExportBook = ActiveWorkbook
    End Sub
    
    
    Private Sub OpenNewWksheet()
    
    '******************************
    '    Open the File Dialog
    '******************************
        Dim ReversionWBook As Workbook  ' Does this need to be module-level scope too?
    
        With Application.FileDialog(msoFileDialogOpen)
            .AllowMultiSelect = False
            .Show
            .Execute
            If .SelectedItems.Count = 0 Then
                MsgBox "User Cancelled Operation"
            End If
        End With
        'ActiveWorkbook.Activate ' This is redundant - the ActiveWorkbook is already active
        Set ReversionWBook = ActiveWorkbook
    End Sub
    
    Private Sub CloseWkbook()
    
    '**********************************
    '  Close Alpha Export WorkBook
    '**********************************
        'You don't need to activate the workbook before you close it
        'AlphaExportBook.Activate
        Application.DisplayAlerts = False
        AlphaExportBook.Close SaveChanges:=False
        Application.DisplayAlerts = True
    End Sub 
    

    或者,您可以在子例程之间传递工作簿对象,如下所示:

    Option Explicit
    
    Sub TestSharedVars()
        'Dimension object to have scope only within this subroutine, but we
        '  will pass a reference to this object to the other subroutines that
        '  need to reference it
        Dim AlphaExportBook As Workbook
        CopyCellsthenClose AlphaExportBook
        OpenNewWksheet
        CloseWkbook AlphaExportBook
    End Sub
    
    Private Sub CopyCellsthenClose(wb As Workbook)
        Dim theRows
        Dim theColumns
        With ActiveSheet.UsedRange
            theRows = .Rows.Count
            theColumns = .Columns.Count
            'Note - the following line won't do what you expect unless
            '       UsedRange starts at cell A1
            Range(Cells(1, 1), Cells(theRows, theColumns)).Select
        End With
        Selection.Copy
    
        Set wb = ActiveWorkbook
    End Sub
    
    
    Private Sub OpenNewWksheet()
    
    '******************************
    '    Open the File Dialog
    '******************************
        Dim ReversionWBook As Workbook  ' Does this need to be module-level scope too?
    
        With Application.FileDialog(msoFileDialogOpen)
            .AllowMultiSelect = False
            .Show
            .Execute
            If .SelectedItems.Count = 0 Then
                MsgBox "User Cancelled Operation"
            End If
        End With
        'ActiveWorkbook.Activate ' This is redundant - the ActiveWorkbook is already active
        Set ReversionWBook = ActiveWorkbook
    End Sub
    
    Private Sub CloseWkbook(wb As Workbook)
    
    '**********************************
    '  Close Alpha Export WorkBook
    '**********************************
        'You don't need to activate the workbook before you close it
        'wb.Activate
        Application.DisplayAlerts = False
        wb.Close SaveChanges:=False
        Application.DisplayAlerts = True
    End Sub 
    

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 2016-10-20
      • 2010-09-13
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多