为用户select的工作簿使用模块variables

没有真正有机会使用模块variables之前,不幸的是,即使在阅读后,我似乎无法使其工作。

我的问题是,我试图使用一个variables分配作为一个工作簿,用户浏览和使用它在不同的潜艇。

作为我的意思的例子代码:

Sub GetFile() Dim fNameAndPath As Variant fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened") If fNameAndPath = False Then Exit Sub Call one Call two End Sub Sub one() With fNameAndPath 'do stuff End With End Sub Sub two() With fNameAndPath 'do stuff End With End Sub 

最好的做法是将variables的范围(在可见的地方)限制在最小的区域 – 在这种情况下,将variables作为参数发送,这样variables只能存在于声明或使用variables的子元素中。

 If fNameAndPath = False Then Exit Sub one fNameAndPath two fNameAndPath End Sub 

并接收他们:

 Sub one(fNameAndPath as Variant) /// Sub two(fNameAndPath as Variant) /// 

With fNameAndPath是无效的,因为fNameAndPath是一个string不是一个对象)