使用公共变量存储路径,并在另一个模块中使用它

时间:2017-02-22 09:51:07

标签: excel vba excel-vba variables

我有一个包含不同模块的工作簿,每个模块都有不同的用途。

代码的作用: 第一个模块打开一个FileDialog框,要求用户选择源文件(.csv)所在的路径,然后搜索并将每个源文件的名称粘贴到主工作簿中。

第二个模块读取文件名并使用“pathS”变量;从第一个模块中获取源工作簿中的信息。

第三个模块清除所有信息并将其输出到新工作簿。

我已尝试的内容:变量pathS在第一个模块中定义为源文件的位置,然后在模块2中用于定位文件(用于信息提取)。我在模块1中宣布它是公共的,在sub之外。

问题:当我运行模块2时,它无法找到源文件。

问题:我使用变量的方式有什么问题吗?

相关代码:

第1单元:

Option Explicit

Global pathS As String

Sub Counter()

Dim count As Integer, i As Long, var As Integer
Dim ws As Worksheet
Dim w As Workbook
Dim Filename As String
Dim FileTypeUserForm As UserForm
Dim x As String
Dim varResult As Variant
Dim OutPath As String, OutPathS As String, wPos As Long

Set w = ThisWorkbook

Application.Calculation = xlCalculationManual

 'source input by user

        varResult = Application.GetSaveAsFilename(FileFilter:="Comma Separated Values Files" & "(*.csv), *.csv", Title:="OutPath", InitialFileName:="D:StartingPath")

        If varResult <> False Then
            OutPath = varResult
            w.Worksheets("FILES").Cells(1, 4) = varResult

        Else

            Exit Sub

        End If


wPos = InStr(OutPath, "\StartingPath")
OutPathS = Mid(OutPath, 1, wPos - 1)

pathS = OutPathS & "\*.*"

Filename = Dir(pathS)

ThisWorkbook.Sheets("FILES").Range("A:A").ClearContents


x = GetValue
If x = "EndProcess" Then Exit Sub


Set ws = ThisWorkbook.Sheets("FILES")
i = 0
Do While Filename <> ""
    var = InStr(Filename, x)

    If var <> 0 Then
        i = i + 1
        ws.Cells(i + 1, 1) = Filename
        Filename = Dir()

    Else: Filename = Dir()
    End If

Loop

Range("A2:A" & i).Sort key1:=Range("A2"), order1:=xlAscending, Header:=xlNo 'this will sort the names directly in the "FILES" sheet

Application.Calculation = xlCalculationAutomatic

ws.Cells(1, 2) = i

MsgBox i & " : files found in folder"
End Sub

第2单元(仅限于错误行):

Sub Gatherer()
Dim w As Workbook
Dim w2 As Workbook
Dim start1 As Long, end1 As Long, end2 As Long, i As Long, lRow As Long, lColumn As Long, t As Long, k As Long, position As Long, g As Long, C As Long
Dim WBArray() As Variant
Dim Cell As Range
Dim ws As Worksheet

Dim MyFolder As String
Dim MyFile As String

Set w = ThisWorkbook

'clean all worksheets in the main file (except FILES), and set date format
For Each ws In w.Worksheets
    If ws.Name <> "FILES" Then
        ws.UsedRange.ClearContents
        ws.Range("D1", "XFD1").NumberFormat = "yyyy-mm-dd"
    End If
Next ws

'Optimize Macro Speed Start
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual  

'opens the first workbook file
For i = 2 To ThisWorkbook.Sheets("FILES").Cells(1, 2).Value + 1 'has to be +1, otherwise the last source file is not accounted for

'error is in this next line, it does not find the file
    Workbooks.Open Filename:=pathS & ThisWorkbook.Sheets("FILES").Cells(i, 1).Value 

1 个答案:

答案 0 :(得分:0)

您可以将模块名称添加到变量前面,以确定您尝试访问哪个变量,以及您是否可以访问它。

示例正常工作:

模块1

Option Explicit

Public foo As String

Sub Test1()

    Module1.foo = "bar"
    Module2.Test2

End Sub

单词数

Option Explicit

Sub Test2()

    MsgBox Module1.foo

End Sub