简体   繁体   中英

Sub or Function in VBA Excel

I'm trying to create a function or sub to create a report in 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. Here is my code:

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:

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...

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.

And

Set WS = Sheets.Add.Name = FullName

should probably be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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