我尝试创建一个函数或子函数来在VBA Excel中创建报表。我希望用户在两个单独的文本框中输入两个日期。然后,当单击提交按钮时,它会检查文本框是否为空,然后如果它们不预先创建CreateReport()Sub或创建新工作表并向其附加数据的函数。这是我的代码:
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
模块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
答案 0 :(得分:3)
当没有输入日期时,您正在呼叫CreateReport
另外
CreateReport(Value1,Value2)
应该是
CreateReport Value1, Value2
除非您正在调用函数或使用Call关键字,否则不要使用括号。
和
Set WS = Sheets.Add.Name = FullName
应该是
Set WS = Sheets.Add()
WS.Name = FullName