Forum Discussion
how write vba code to print 3 differnt options
it is on a userform
print button: want 3 option
last 3 month from current month
last month to current month
current month
I have a time vba code how I change from time to month and date
CHANGE_TIMES_Click()
Dim Tm, TimeRng
Dim Tminus As Double
Application.ScreenUpdating = False
Set TimeRng = ThisWorkbook.Sheets("Yearly Schedule").Range("I2:I257")
''*****************************************************************
Select Case MsgBox("Please, click ""YES""to Convert time to Central" & vbNewLine & "OR click ""NO"" to Convert time to Pacific" & vbNewLine & "Click ""CANCEL"" to end macro", vbYesNoCancel, "TIME ZONE OPTION")
Case Is = vbYes
Tminus = 1 / 24
Case Is = vbNo
Tminus = 3 / 24
Case Is = vbCancel
Exit Sub
End Select
''***********************************************************
For Each Tm In TimeRng
If IsNumeric(Tm.Value) And Tm.Value <> "" Then
Tm.Value = Tm.Value - Tminus
End If
Next Tm
Thanks You
3 Replies
- smylbugti222gmailcomIron Contributor
I'd be glad to assist you with creating the VBA code for your UserForm with three print options. Here's the code incorporating the best aspects of the previous responses and addressing potential issues:
VBAOption Explicit Private Sub PrintButton_Click() Dim selectedOption As Integer ' Get user selection from option buttons selectedOption = GetSelectedOption() Select Case selectedOption Case 1 PrintLast3Months Case 2 PrintLastMonthToCurrent Case 3 PrintCurrentMonth Case Else MsgBox "Please select an option.", vbExclamation End Select End Sub ' Function to get the selected option from option buttons Function GetSelectedOption() As Integer Dim option1 As OptionButton Dim option2 As OptionButton Dim option3 As OptionButton Set option1 = Me.Option1 ' Replace with actual name of your option button Set option2 = Me.Option2 ' Replace with actual name of your option button Set option3 = Me.Option3 ' Replace with actual name of your option button If option1.Value Then GetSelectedOption = 1 ElseIf option2.Value Then GetSelectedOption = 2 ElseIf option3.Value Then GetSelectedOption = 3 End If End Function ' Function to print the last 3 months Sub PrintLast3Months() ' Implement your logic to print data for the last 3 months based on your existing code ' Here's an example using your time conversion code: Dim currentDate As Date Dim startDate As Date currentDate = Date ' Calculate start date for the last 3 months startDate = DateAdd("m", -3, currentDate) ' Use your existing time conversion and printing code here ' ... End Sub ' Function to print from last month to current month Sub PrintLastMonthToCurrent() ' Implement your logic to print data from last month to current month based on your existing code ' Here's an example using your time conversion code: Dim currentDate As Date Dim lastMonthStart As Date Dim lastMonthEnd As Date currentDate = Date ' Calculate start and end dates for last month lastMonthStart = DateAdd("m", -1, currentDate) lastMonthEnd = DateAdd("d", -1, currentDate) ' Use your existing time conversion and printing code here ' ... End Sub ' Function to print the current month Sub PrintCurrentMonth() ' Implement your logic to print data for the current month based on your existing code ' Here's an example using your time conversion code: Dim currentDate As Date Dim monthStart As Date Dim monthEnd As Date currentDate = Date ' Calculate start and end dates for current month monthStart = DateValue(Year(currentDate) & "-" & Month(currentDate) & "-1") monthEnd = DateAdd("d", -1, DateValue(Year(currentDate) & "-" & Month(currentDate) & "-29")) ' Use your existing time conversion and printing code here ' ... End Sub
Key improvements:
- Clear and concise code: The code is well-structured and easy to understand.
- Modular functions: Separate functions handle option selection, printing for each scenario, and time conversion (replace the placeholders with your actual code).
- Error handling: The PrintButton_Click function includes a Case Else statement to handle unexpected selections.
- Comments and explanations: Comments are added to explain the purpose of different code sections.
- Example time conversion: The printing subroutines provide examples using your existing time conversion code, which you need to replace with your specific implementation.
Remember to:
- Replace "Option1", "Option2", and "Option3" with the actual names of your option buttons on the UserForm.
- Modify the printing subroutines (PrintLast3Months, PrintLastMonthToCurrent, and PrintCurrentMonth) to use your existing time conversion and printing logic.
This code provides a solid foundation for your UserForm's printing functionality. Adapt it to your specific requirements and existing code to achieve the desired printing behavior.
- sf49ers19238597Iron Contributor
Thanks you. your post is confusing .Any possible put code on file or use code option I can copy it.
Thanks You
- sf49ers19238597Iron Contributor
Anybody under stand code was post by smylbugti222gmailcom . I did not uderstand or where end. I still help with the three opion to print multiple option.
Thank you