我的代码作为单独的程序工作得很好。我决定将这两个过程结合起来,因为代码大致相同,但有一些区别在于将单元格复制到数组以保存到不同的日志表。每个程序都有一个Inputbox
来获取感兴趣的年份(2014年,2015年,2016年....),用于在日志不存在的情况下打开AND。使用年份代码在文件名中。
根据单元格值,我正在执行If_Then_Else
以确定运行一个过程或两者。如果我将它们作为单独的程序单独运行它们可以正常工当我合并它们时,我决定从内部调用一个过程(SaveToLog_Audit
)(SaveToLog_Replc
)。我从(SaveToLog_Audit
)中删除了日期输入,让(SaveToLog_Replc
)获取该日期代码并传递它。我不希望(SaveToLog_Audit
)第二次要求约会,因为我基本上希望它是不引人注意的(当我可以通过Ans
时,不需要再问两次它
我尝试过设置公开Ans
并在开头使用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
答案 0 :(得分:1)
您是否正如您发布的那样运行代码?如果是,那么问题可能是你在公开ans然后你在第一个sub中使用dim ans,之后你将值赋值给dim Ans而不是public Ans这些是两个单独的变量。 从两个子程序中删除昏暗的Ans,它应该可以正常工作
答案 1 :(得分:1)
我建议您删除Public声明并更改第二个例程以获取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