如何在所有子例程中使用宏变量

时间:2019-05-11 22:17:26

标签: excel vba

我有一个可变的文件名,在子例程之一中定义。如何在同一模块内任何其他子例程的其他地方全局使用它

Sub database_create()
    Dim FileFormat As String
    Dim LastRow As Long
    Dim i As Integer
    'Find the last row.
    With shPaths
        LastRow = .Cells(.Rows.count, "B").End(xlUp).Row
    End With
'    For i = 2 To LastRow
    For i = 2 To 2
    conversion_importexcel Cells(i, 3).Value

    Next
    'Inform the user that conversion finished.
    MsgBox "All files were converted successfully!", vbInformation, "Finished"
End Sub


Sub conversion_importexcel(OutPath As String)
Dim lRow As Long
Dim lCol As Long
Dim fNameAndPath As Variant
Dim filename As Variant
Dim convtexcelpath As String
Dim frstwrd As String
'convtexcelpath = WorksheetFunction.Substitute(OutPath, ".pdf", "_adobeConverted" & "." & LCase(FileExtension))
convtexcelpath = WorksheetFunction.Substitute(OutPath, ".pdf", "_Converted" & "." & "xlsx")

fNameAndPath = convtexcelpath
If fNameAndPath = False Then Exit Sub
Workbooks.Open filename:=fNameAndPath

filename = fNameAndPath
filename = (Replace(Mid(filename, InStrRev(filename, "\") + 1), ".xlsx", "") & "_database")
end sub

1 个答案:

答案 0 :(得分:1)

您可以在模块顶部声明它们,以便可以在该模块中的所有过程中使用它们。

Option Explicit
Global FileName as String
Global i as Long

Sub database_create()
'some code here
End Sub