VBA在另一个模块内调用模块时,变量无法识别

时间:2016-11-01 14:45:12

标签: vba excel-vba excel

我试图将工作表保存到本地文件夹中。当我使用这段代码时,一切运作良好:

Sub SavePrepAs()
Workbooks("Robot Model.xlsm").Activate
LocalPath = ActiveWorkbook.Worksheets("Preparation").Range("B6").Value
If Right(LocalPath, 1) <> "\" Then LocalPath = LocalPath & "\"

Workbooks("Robot Model.xlsm").Worksheets("Preparation").Copy
With ActiveWorkbook
    .SaveAs LocalPath & .Sheets(1).Name
    .Close 0
End With
End Sub

但是当我尝试将代码分成两个子代码时,文件被保存到我的H驱动器中,而不是像我在B6单元格中指示的H驱动器的子文件夹。为什么呢?

Sub ReadLocalPath()

'Read Local Path from the sheet
Workbooks("Robot Model.xlsm").Activate
LocalPath = ActiveWorkbook.Worksheets("Preparation").Range("B6").Value
If Right(LocalPath, 1) <> "\" Then LocalPath = LocalPath & "\"
Debug.Print LocalPath
End Sub

Sub SavePrepAs()
Call ReadLocalPath

Workbooks("Robot Model.xlsm").Worksheets("Preparation").Copy
With ActiveWorkbook
    .SaveAs LocalPath & .Sheets(1).Name
    .Close 0
End With
Debug.Print LocalPath
End Sub

0 个答案:

没有答案