如何调用带参数的子程序?
How to call a subroutine that has parameters?
我正在开发一个 Excel 用户表单来为给定日期输入的批次生成报告。
报告存储在 Word 文档中,其中包含 1 到 8 个质量样本的结果(样本数量因批次而异)。
Userform 旨在加载 Excel,从用户那里接收批号和日期,从 Excel 中的不同 sheet 中检索当天的样本和批次工作簿,然后根据自定义模板将数据复制到新的 Word 文档中。
Userform的输入部分和Word模板都设置好了。我在“确定”按钮的事件处理过程中遇到了障碍。
表单的确定按钮事件处理程序给出
compile error
在
Sub makeReport(lNum As Integer, pDay As Date)
编辑器没有指出我的 makeReport
方法有问题;事件处理程序中对 makeReport
的调用以红色突出显示。
我正在使用 Excel 2013 VBA 编辑器,而不是 Excel 中的内置调试工具、Microsoft 在线 VBA 文档和各种论坛帖子通过 Google 找到的可以给我一个完整的答案,告诉我什么是错误的以及如何解决它。
确定按钮事件处理程序
Private Sub OKButton_Click() 'OK button
'Declare variables
Dim lNum As Integer
Dim pDay As Date
Dim name As String
Dim nStr As String
Dim dStr As String
'Error handler for incorrect input of lot number or pack date
On Error GoTo ErrorHandler
'Convert input values to correct types
nStr = TextBox1.Value
dStr = TextBox2.Value
'Set variable values
lNum = CInt(nStr)
MsgBox ("Step 1 Done" + vbCrLf + "Lot Number: " + nStr)
pDay = Format(dStr, "mm/dd/yyyy")
MsgBox ("Step 2 Done" + vbCrLf + "Pack Date: " + dStr)
name = nameDoc(pDay, lNum)
MsgBox ("Step 3 Done" + vbCrLf + "Report Name: " + name)
'Check for existing report
If Dir("\CORE\Miscellaneous\Quality\Sample Reports\" + name) Then
MsgBox ("The file " + name + "already exists. Check \CORE\Miscellaneous\Quality\Sample Reports for the report.")
Unload UserForm1
Exit Sub
Else
makeReport(lNum, pDay)
End If
'Unload User Form and clean up
Unload UserForm1
Exit Sub
ErrorHandler:
MsgBox ("Error. Please Try Again.")
'Unload UserForm1
End Sub
makeReport 子
Sub makeReport(lNum As Integer, pDay As Date)
'Template Path: \CORE\Miscellaneous\Quality\Sample Reports\Template\Defect Report.dotm
'Save path for finished report: \CORE\Miscellaneous\Quality\Sample Reports
'Generate doc name
Dim name As String
name = nameDoc(pDay, lNum)
'Initialize word objects and open word
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Add(Template:=("\CORE\Miscellaneous\Quality\Sample Reports\Template\Defect Report.dotm"), NewTemplate:=False, DocumentType:=0)
'Initialize excel objects
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Defect Table")
'Fill in lot number and date at top of report
With wDoc
.Application.Selection.Find.Text = "<<date>>"
.Application.Selection.Find.Execute
.Application.Selection = pDay
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<lot>>"
.Application.Selection.Find.Execute
.Application.Selection = lNum
End With
'Initialize loop variables
Dim row1 As Integer
Dim row2 As Integer
Dim diff As Integer
Dim more As Boolean
Dim num As Integer, num1 As Integer, col As Integer
Dim count As Integer
count = 0
diff = 0
more = False
'Do while loop allows variable number of samples per day
Do While count < 8
'Checks for correct starting row of day
row1 = WorksheetFunction.Match(lNum, wsSheet.Range(), 0)
row2 = WorksheetFunction.Match(pDay, wsSheet.Range(), 0)
If IsError(row1) Or IsError(row2) Then
'Breaks for loop once all samples have been copied over
Exit Do
ElseIf row1 = row2 Then
num = 4
num1 = num
Do While num < 31
'Column variable
col = count + 1
'Copies data to word doc, accounting for blank rows in the word table
Select Case num
Case 6, 10, 16, 22, 30
num1 = num1 + 1
Case Else
num1 = num1
End Select
ActiveDocument.Tables(1).Cell(num1, col) = ActiveSheet.Range().Cells(row1, num)
num = num + 1
Next
Else
'Deiterates count to adjust for differences between row1 and row2
count = count - 1
End If
'Moves the collision to below row1 to allow MATCH to find next viable result
diff = row1 + 1
wsSheet = wsSheet.Range().Offset(diff, 0)
'Iterates count
count = count + 1
Loop
'Zeroes out word objects
Set wdDoc = Nothing
Set wdApp = Nothing
'Saves Document using regular name format for ease of access
wDoc.SaveAs2 Filename:="\CORE\Miscellaneous\Quality\Sample Reports\" + name, FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
End Sub
makeReport(lNum, pDay)
此处的括号表示您希望返回某些东西,但这是不可能发生的,因为 makeReport
是 Sub
而不是 Function
。这导致编译错误。要更正,只需删除括号。
您还有一个问题,因为与 pDay
不匹配。当您格式化日期时,您将它从 Date
(只是一个数值)转换为 String
.
在 OKButton_Click()
中尝试更改:
pDay = Format(dStr, "mm/dd/yyyy")
至:
pDay = CDate(dStr)
以便它匹配 makeReport
期望的数据类型。然后,您可以通过更改
在 makeReport
中应用所需的格式
.Application.Selection = pDay
到
.Application.Selection = Format(pDay, "mm/dd/yyyy")
我正在开发一个 Excel 用户表单来为给定日期输入的批次生成报告。
报告存储在 Word 文档中,其中包含 1 到 8 个质量样本的结果(样本数量因批次而异)。
Userform 旨在加载 Excel,从用户那里接收批号和日期,从 Excel 中的不同 sheet 中检索当天的样本和批次工作簿,然后根据自定义模板将数据复制到新的 Word 文档中。
Userform的输入部分和Word模板都设置好了。我在“确定”按钮的事件处理过程中遇到了障碍。
表单的确定按钮事件处理程序给出
compile error
在
Sub makeReport(lNum As Integer, pDay As Date)
编辑器没有指出我的 makeReport
方法有问题;事件处理程序中对 makeReport
的调用以红色突出显示。
我正在使用 Excel 2013 VBA 编辑器,而不是 Excel 中的内置调试工具、Microsoft 在线 VBA 文档和各种论坛帖子通过 Google 找到的可以给我一个完整的答案,告诉我什么是错误的以及如何解决它。
确定按钮事件处理程序
Private Sub OKButton_Click() 'OK button
'Declare variables
Dim lNum As Integer
Dim pDay As Date
Dim name As String
Dim nStr As String
Dim dStr As String
'Error handler for incorrect input of lot number or pack date
On Error GoTo ErrorHandler
'Convert input values to correct types
nStr = TextBox1.Value
dStr = TextBox2.Value
'Set variable values
lNum = CInt(nStr)
MsgBox ("Step 1 Done" + vbCrLf + "Lot Number: " + nStr)
pDay = Format(dStr, "mm/dd/yyyy")
MsgBox ("Step 2 Done" + vbCrLf + "Pack Date: " + dStr)
name = nameDoc(pDay, lNum)
MsgBox ("Step 3 Done" + vbCrLf + "Report Name: " + name)
'Check for existing report
If Dir("\CORE\Miscellaneous\Quality\Sample Reports\" + name) Then
MsgBox ("The file " + name + "already exists. Check \CORE\Miscellaneous\Quality\Sample Reports for the report.")
Unload UserForm1
Exit Sub
Else
makeReport(lNum, pDay)
End If
'Unload User Form and clean up
Unload UserForm1
Exit Sub
ErrorHandler:
MsgBox ("Error. Please Try Again.")
'Unload UserForm1
End Sub
makeReport 子
Sub makeReport(lNum As Integer, pDay As Date)
'Template Path: \CORE\Miscellaneous\Quality\Sample Reports\Template\Defect Report.dotm
'Save path for finished report: \CORE\Miscellaneous\Quality\Sample Reports
'Generate doc name
Dim name As String
name = nameDoc(pDay, lNum)
'Initialize word objects and open word
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Add(Template:=("\CORE\Miscellaneous\Quality\Sample Reports\Template\Defect Report.dotm"), NewTemplate:=False, DocumentType:=0)
'Initialize excel objects
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Defect Table")
'Fill in lot number and date at top of report
With wDoc
.Application.Selection.Find.Text = "<<date>>"
.Application.Selection.Find.Execute
.Application.Selection = pDay
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<lot>>"
.Application.Selection.Find.Execute
.Application.Selection = lNum
End With
'Initialize loop variables
Dim row1 As Integer
Dim row2 As Integer
Dim diff As Integer
Dim more As Boolean
Dim num As Integer, num1 As Integer, col As Integer
Dim count As Integer
count = 0
diff = 0
more = False
'Do while loop allows variable number of samples per day
Do While count < 8
'Checks for correct starting row of day
row1 = WorksheetFunction.Match(lNum, wsSheet.Range(), 0)
row2 = WorksheetFunction.Match(pDay, wsSheet.Range(), 0)
If IsError(row1) Or IsError(row2) Then
'Breaks for loop once all samples have been copied over
Exit Do
ElseIf row1 = row2 Then
num = 4
num1 = num
Do While num < 31
'Column variable
col = count + 1
'Copies data to word doc, accounting for blank rows in the word table
Select Case num
Case 6, 10, 16, 22, 30
num1 = num1 + 1
Case Else
num1 = num1
End Select
ActiveDocument.Tables(1).Cell(num1, col) = ActiveSheet.Range().Cells(row1, num)
num = num + 1
Next
Else
'Deiterates count to adjust for differences between row1 and row2
count = count - 1
End If
'Moves the collision to below row1 to allow MATCH to find next viable result
diff = row1 + 1
wsSheet = wsSheet.Range().Offset(diff, 0)
'Iterates count
count = count + 1
Loop
'Zeroes out word objects
Set wdDoc = Nothing
Set wdApp = Nothing
'Saves Document using regular name format for ease of access
wDoc.SaveAs2 Filename:="\CORE\Miscellaneous\Quality\Sample Reports\" + name, FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
End Sub
makeReport(lNum, pDay)
此处的括号表示您希望返回某些东西,但这是不可能发生的,因为 makeReport
是 Sub
而不是 Function
。这导致编译错误。要更正,只需删除括号。
您还有一个问题,因为与 pDay
不匹配。当您格式化日期时,您将它从 Date
(只是一个数值)转换为 String
.
在 OKButton_Click()
中尝试更改:
pDay = Format(dStr, "mm/dd/yyyy")
至:
pDay = CDate(dStr)
以便它匹配 makeReport
期望的数据类型。然后,您可以通过更改
makeReport
中应用所需的格式
.Application.Selection = pDay
到
.Application.Selection = Format(pDay, "mm/dd/yyyy")