I am having some issues with the following: In my sheet 'Overtime' a form 'TimeForm1' is…
- User
I am having some issues with the following: In my sheet 'Overtime' a form 'TimeForm1' is activated. When the form opens, I insert a StartTime value and an EndTime value. When I click OK on the form button, it is supposed to writre the values I entered into the ActiveCell.Offset(-1, 28), and to the ActiveCell.Offset(-1, 29) of the 'Overtime' sheet. Sometimes, it will write the values to the exact location, but most of the time it does not. Also sometimes when I reactivate the form, the previous values entered in the text boxes are still there. Can you please help me to solve this problem. I have included below, the VBA code from the sheet 'Overtime' and the code from 'TimeForm1'.
Overtime Code:
Option Explicit
Private Sub Worksheet_Activate() Dim activeCellRange As Range Set activeCellRange = Nothing End Sub
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 And IsNumeric(Target.Value) Then ShowTimeForm Target.Address End If End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not TimeForm1 Is Nothing Then TimeForm1.Hide End If End Sub
Sub ShowTimeForm(ByVal rngAddress As String) TimeForm1.ActiveCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub
TimeForm1 - Code:
Public Property Let ActiveCellRange(rng As Range) Set newActiveCellRange = rng End Property
Public Property Get ActiveCellRange() As Range Set ActiveCellRange = ActiveCellRange End Property
Private Sub UserForm_Initialize() Me.ActiveCellRange = ActiveCell End Sub
Private Sub StartEndButton1_Click() ActiveCell.Offset(-1, 28).Value = StartTime.Value ActiveCell.Offset(-1, 29).Value = EndTime.Value Me.Hide End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then Cancel = True Me.Hide End If End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Set ActiveCellRange = Target End Sub
- Assistant
There are a couple of issues in your code that may be causing the problems you mentioned:
-
In the Overtime code, the
Worksheet_Activateevent is not doing anything. You can remove it. -
In the Overtime code, the
ShowTimeFormsub is not properly setting theactiveCellRange. It should set theactiveCellRangevariable instead of theTimeForm1property. Modify it as follows:
Sub ShowTimeForm(ByVal rngAddress As String) Set activeCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub- In TimeForm1 code, the
ActiveCellRangeproperty is not correctly defined. The property should have the same name as the variable used internally. Change it as follows:
Public Property Get ActiveCellRange() As Range Set ActiveCellRange = newActiveCellRange End Property -
- User
I am getting an error: Variable not defined on 'ActiveCellRange' in the following event: Sub ShowTimeForm(ByVal rngAddress As String) Set ActiveCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub
- Assistant
I apologize for the confusion. In the
Sub ShowTimeFormcode, the variable nameActiveCellRangeconflicts with the property nameActiveCellRangein theTimeForm1code.To resolve this issue, you can rename the variable
activeCellRangein theOvertimecode to avoid the conflict.Here's the updated code:
Option Explicit Private activeCellRange As Range Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 And IsNumeric(Target.Value) Then ShowTimeForm Target.Address End If End Sub Sub ShowTimeForm(ByVal rngAddress As String) Set activeCellRange = Me.Range(rngAddress) TimeForm1.ActiveCellRange = activeCellRange TimeForm1.Show vbModal End SubAdditionally, in the
TimeForm1code, make sure that the variablenewActiveCellRangeis properly declared at the beginning of the module.Private newActiveCellRange As Range Public Property Let ActiveCellRange(rng As Range) Set newActiveCellRange = rng End Property Public Property Get ActiveCellRange() As Range Set ActiveCellRange = newActiveCellRange End Property - User
Is this code correct: OTFormSheet.Range("I24").Value = TODAY()
- Assistant
The code you provided has a minor syntax issue. The function
TODAY()should be changed toDatein VBA. Here's the corrected code:OTFormSheet.Range("I24").Value = DateThis code will write the current date to cell I24 in the worksheet
OTFormSheet. - User
I want to open a message box that will use the value in OTFormSheet.Range("B4") as beginning of the message. the end part of the message will be ' overtime has been entered'
- Assistant
To open a message box with the value in
OTFormSheet.Range("B4")as the beginning of the message and the end part of the message as "'overtime has been entered'", you can use the following VBA code:Dim message As String message = OTFormSheet.Range("B4").Value & " overtime has been entered" MsgBox messageThis code assigns the concatenation of
OTFormSheet.Range("B4").Valueand "overtime has been entered" to themessagevariable and then displays it in a message box usingMsgBox. - User
With this message is it possible to add vbInformation, "OVERTIME ENTRY"
- Assistant
Yes, you can add a message box style to the message box by using the
MsgBoxfunction. To display the message box with the message "OVERTIME ENTRY" and include the value fromOTFormSheet.Range("B4")as the message text, you can use the following code:Dim message As String message = OTFormSheet.Range("B4").Value & " overtime has been entered" MsgBox message, vbInformation, "OVERTIME ENTRY"This code will open a message box with the specified message text, the message box style as
vbInformation(exclamation point icon), and "OVERTIME ENTRY" as the title of the message box. - User
Is this VBA code OK to activate a print command or is there a simpler code: Sub printOvertime() ActiveSheet.PageSetup.PrintArea = "I26" Application.PrintCommunication = True ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False Selection.AutoFilter End Sub
- Assistant
The code you provided is generally correct for printing a specific range, but there are a few things to consider:
-
It's recommended to explicitly state the worksheet you want to print. Instead of using
ActiveSheet, specify the worksheet name or object. -
The line
Application.PrintCommunication = Trueis not necessary as it is the default setting. -
The line
Selection.AutoFilteris not necessary for printing and may not have any effect on the printed output.
Here's an updated version of your code:
Sub printOvertime() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("YourWorksheetName") ws.PageSetup.PrintArea = "$A1:$I26" ws.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False End SubIn this code, replace
"YourWorksheetName"with the actual name of your worksheet that you want to print. This code sets the print area, prints the specified range, and ignores any print areas defined in the worksheet. The linews.PrintOutwill print the worksheet directly without the need to select it. -