无法在同一个模块中将variables从一个子项传递给另一个子项

我的代码作为单独的程序很好。 我决定结合这两个程序,因为代码大部分是相同的,其中单元格被复制到数组以保存到不同的日志表中。 每个程序都有一个Inputbox来获取将被用来打开和日志不存在的年份(2014年,2015年,2016年…)。如果日志不存在,则使用年份代码文件名。

基于一个单元格的值,我正在做一个If_Then_Else来确定运行一个程序或两者。 如果我单独运行它们作为单独的程序,他们工作正常。 当我结合他们时,我决定从里面( SaveToLog_Replc )调用一个程序( SaveToLog_Replc )。 我从( SaveToLog_Audit )中删除了Dateinput,让( SaveToLog_Replc )获取该date代码并传递给它。 我不希望( SaveToLog_Audit )在运行时SaveToLog_Audit要求一个date,因为我基本上希望它是侵入式的(当我可以将Ans传递给它时不需要再问两次。

我已经尝试设置公共Ans并在同一模块中的两个过程开始使用Explicit – >没有工作。 在选项显式 – >没有工作之后,我尝试了Dim Ans As String。 我尝试公共答案作为string – >没有工作。 很多我都无法通过variables“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 

你是否按照你发布的方式运行代码? 如果是,那么问题可能是你正在拖延公众的答案,然后你在第一个子使用dim ans,稍后你分配的值昏暗的答案不公开Ans这些是由两个单独的variables。 从两个子例程中删除模糊答案,它应该工作

我build议你删除公共声明,并更改第二个例程以采取一个string参数:

 Option Explicit 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(Ans) ' 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(Ans) ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx End If End Sub Public Sub SaveToLog_Audit(Ans As String) ' Date entry for proper Dated log save 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