2017-01-24 3 views
0

Ich versuche, den Namen einer Arbeitsmappe, die ich in Modul 1 festgelegt, über andere private Module zu verwenden, aber ich bekomme verschiedene Fehler, je nachdem, wie ich es eingerichtet habe. Ich fügte Kommentare in den Code ein, die erklären, was in den verschiedenen Szenarien passiert.VBA Verwenden Sie Variablenwert über Module

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 

Antwort

1

Erstens sollten Sie nicht ein „Argument nicht optional“ Fehler bekommen, wenn OpenNewWksheet Aufruf, weil das Unterprogramm nicht Argumente erwartet wird. Sie würden diesen Fehler erhalten, wenn Sie versuchen würden, CloseWkbook aufzurufen, ohne einen Parameter anzugeben, weil dieses Unterprogramm erwartet, dass ein Workbook Objekt an es übergeben wird.


Der einfachste Weg, um die Arbeitsmappe zur Verfügung zu allen Subroutinen zu machen, ist die Variable mit Modulebene Umfang zu erklären, z.B.

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 

Alternativ können Sie das Arbeitsobjekt zwischen Subroutinen übergeben, wie folgt:

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 
Verwandte Themen