【发布时间】:2015-08-02 16:33:26
【问题描述】:
我拥有的代码可以作为单独的程序很好地工作。我决定将这 2 个过程结合起来,因为代码几乎相同,但在将单元格复制到数组以保存到不同的日志表中存在一些差异。每个程序都有一个Inputbox 来获取感兴趣的年份(2014、2015、2016 ......),如果日志不存在,将用于打开 AND 使用年份代码从主服务器保存一个新的在文件名中。
基于单元格值,我正在执行If_Then_Else 以确定运行一个过程或两者。如果我将它们作为单独的程序单独运行,它们可以正常工作。当我将它们组合在一起时,我决定从内部 (SaveToLog_Replc) 调用一个过程 (SaveToLog_Audit)。我从 (SaveToLog_Audit) 中删除了日期输入,让 (SaveToLog_Replc) 获取该日期代码并传递它。我不想让 (SaveToLog_Audit) 在它运行时第二次要求约会,因为我基本上希望它不显眼(当我可以将 Ans 传递给它时,无需询问两次。
我尝试设置 public Ans 并在开始时使用 Explicit 与同一模块中的两个过程 --> 不起作用。我在选项 Explicit 之后尝试了 Dim Ans As String --> 不起作用。我尝试将 Public Ans as String --> 没有用。无论我尝试什么,我几乎都无法让它传递变量“Ans”。我对 VBA 编码还是很陌生,所以任何帮助都会很棒。显示基本代码,只显示有问题的部分。
Option Explicit
Public Ans As String
Public Sub SaveToLog_Replc()
' Determine scrap and whether to save to Replacement_Credit tracking log
Dim ScrapQty As Range
' Date entry for proper Dated log save
Dim Ans As String
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Set ScrapQty = Worksheets("Main").Range("M_Qty_Scrap")
Application.ScreenUpdating = False
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Ans = InputBox("Enter Log Year" & _
"" & vbCrLf, "Year Selection", Format(Date, "YYYY"))
If Ans = "" Then
Exit Sub
End If
' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' If scrap qty is greater than Zero (0) save to both Logs
If ScrapQty > 0 Then
' If True
MsgBox "Saving to Replacement Log and Audit Log"
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log then Save to Replacement Log
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Sub routine to Save Data to Replacement Log
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Replc
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' ***************************************************************
'Check if Replacement Log exists. If not open Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\Replacement Log " & Ans & ".xls")
If Not c Then
'MsgBox "The file doesn't exist!"
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_MASTER Replacement Log.xls"
fName = "Replacement Log " & Ans & ".xls" ' Set fName to new FileName
newFile = fName ' Sets new filename as fName
path = "S:\RECORDS\Logs\" ' Path to Incoming Audit Logs
ActiveWorkbook.SaveAs Filename:=path & newFile ' Saves as newFile
Else
'MsgBox "The file exists! Saving data to it."
Workbooks.Open Filename:="S:\RECORDS\Logs\Replacement Log " & Ans & ".xls"
End If
' ***************************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'Replacement Log " & Ans & ".xls'", vbInformation, "Log Save Confirmation"
' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Else
' If False
MsgBox "Saving to Audit Log Only."
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Save to Audit Log as Normal
' --> 'Ans' VALUE NEEDS TO PASS TO THE PROCEDURE
' --> 'Ans' DOES NOT PASS TO THIS PROCEDURE AT END OF MODULE
Call SaveToLog_Audit
' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
End If
End Sub
Public Sub SaveToLog_Audit()
' Date entry for proper Dated log save
Dim Ans As String
Dim c As Boolean
Dim fs As Object
' Declares path, newFile and fName variables
Dim path As String, newFile As String, fName As String
Set Sheet2 = ActiveSheet
Application.ScreenUpdating = False
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Load Array code here for SaveToLog_Audit
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' For debug
' --> 'Ans' DOES NOT PASS FROM (SaveToLog_Replc) PROCEDURE IN THIS MODULE
MsgBox "The value of Ans is: " & vbCrLf & vbCrLf & "'" & Ans & "'"
' ********************************************************
' Checks if Log File exists. If not open the Master, rename then save it. If it exists open it.
Set fs = CreateObject("Scripting.FileSystemObject")
' Filename to check for
c = fs.fileExists("S:\RECORDS\Logs\" & Ans & " Audit Log.xls")
If Not c Then
'MsgBox "The file doesn't exist!" original code
' File doesn't exist. Open the Blank Master
Workbooks.Open Filename:="S:\RECORDS\Logs\_Master Audit Log.xls"
' Set fName to new FileName
fName = Ans & " Audit Log.xls"
' Sets new filename as fName saves to this directory
newFile = fName
' Path to Incoming Audit Logs
path = "S:\RECORDS\Logs\"
' Saves Blank Master as newFile
ActiveWorkbook.SaveAs Filename:=path & newFile
Else
'Workbooks.Open "C:\filename.xls"
Workbooks.Open Filename:= _
"S:\RECORDS\Logs\" & Ans & " Audit Log.xls"
End If
' ********************************************************
' Unprotect Sheet and Show All Data code here
' Find LastRow. Set NextCell position code here
' Set the size of the new array and copy MyAr code here
' Draw Border Code here
' **********************************************
ActiveWorkbook.Save ' Saves Destination Workbook
ActiveWindow.Close ' Closes Destination Workbook
Application.ScreenUpdating = True
' Confirms Save to Log File
MsgBox "Your Data has been saved to the Log File: " & vbCrLf & vbCrLf _
& "'" & Ans & " Audit Log.xls'", vbInformation, "Log Save Confirmation"
End Sub
【问题讨论】: