简体   繁体   English

VBA Excel中的子或功能

[英]Sub or Function in VBA Excel

I'm trying to create a function or sub to create a report in VBA Excel. 我正在尝试在VBA Excel中创建函数或子报表来创建报告。 I want the user to Enter two dates into two separate text boxes. 我希望用户在两个单独的文本框中输入两个日期。 Then when the submit button is clicked it checks if the text boxes are empty or not then if they aren't it preforms the CreateReport() Sub or function which creates a new sheet and appends data to it. 然后,当单击“提交”按钮时,它将检查文本框是否为空,然后检查文本框是否为空,然后执行CreateReport()子函数或函数,以创建新工作表并将数据附加到该工作表中。 Here is my code: 这是我的代码:

Userform2: Userform2:

Private Sub Cancel_Click()
 Unload Me
End Sub

Private Sub Submit_Click()

If UserForm2.Date1.Value = "" & UserForm2.Date2.Value = "" Then

Value1 = UserForm2.Date1.Value
Value2 = UserForm2.Date2.Value
CreateReport(Value1,Value2)

End If

End Sub

Private Sub UserForm_Initialize()
Date1.SetFocus
Dim Value1 As String
Dim Value2 As String
End Sub

Module1: 模块1:

Option Explicit
Public Function CreateSheet(Name1 As String, Name2 As String)
Dim WS As Worksheet
Dim FullName As String
FullName = Name1 & "-" & Name2
Set WS = Sheets.Add.Name = FullName
End Function

Public Sub CreateReport(Date1 As String, Date2 As String)

End Sub

You're calling CreateReport when there are no dates entered... 您没有输入任何日期时就调用CreateReport ...

Also

CreateReport(Value1,Value2)

should be 应该

CreateReport Value1, Value2

you don't use parentheses unless you're calling a function or using the Call keyword. 除非您要调用函数或使用Call关键字,否则不要使用括号。

And

Set WS = Sheets.Add.Name = FullName

should probably be 应该是

Set WS = Sheets.Add()
WS.Name = FullName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM