When I run this code in my Form Module, it does not copy the values to the correct…
- User
When I run this code in my Form Module, it does not copy the values to the correct location
Private activeCellRange As Range
Private Sub UserForm_Initialize() ' Assign the active cell to the property variable Set activeCellRange = activeCell End Sub
Private Sub StartEndButton1_Click() Dim activeCell As Range Set activeCell = activeCell ' Update the start and end times in columns AD and AE using the stored active cell activeCellRange.Offset(-1, 28).Value = StartTime.Value activeCellRange.Offset(-1, 29).Value = EndTime.Value ' Hide the form Me.Hide End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) ' Cancel the close event to avoid closing the form unintentionally If CloseMode = 0 Then Cancel = True Me.Hide End If End Sub
- Assistant
The issue in your code seems to be with the way you are assigning the activeCellRange variable.
In the UserForm_Initialize event, you are trying to assign the active cell to the activeCellRange variable using the line "Set activeCellRange = activeCell". However, the activeCell variable is not declared or assigned a value anywhere in your code.
To fix this issue, you can remove the line "Dim activeCell As Range" from the StartEndButton1_Click event, as you don't actually need to declare a new activeCell variable.
Here is the modified code:
Private activeCellRange As Range
Private Sub UserForm_Initialize() ' Assign the active cell to the property variable Set activeCellRange = ActiveCell End Sub
Private Sub StartEndButton1_Click() ' Update the start and end times in columns AD and AE using the stored active cell activeCellRange.Offset(-1, 28).Value = StartTime.Value activeCellRange.Offset(-1, 29).Value = EndTime.Value ' Hide the form Me.Hide End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) ' Cancel the close event to avoid closing the form unintentionally If CloseMode = 0 Then Cancel = True Me.Hide End If End Sub
- User
In my worksheet Module, I also have the following code that works with TimeForm1. Do I also need the line of code 'Private activeCellRange As Range' in the worksheet module, and are there any areas of the code that may be affecting the correct functioning of the form.
Option Explicit Private activeCellRange As Range
Private Sub Worksheet_Activate() Set activeCellRange = Nothing End Sub
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 And IsNumeric(Target.Value) Then ShowTimeForm Target 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 rng As Range) Dim Target As Range Set activeCellRange = Target TimeForm1.Show vbModal End Sub
- Assistant
No, you do not need to have the line "Private activeCellRange As Range" in the worksheet module. The line is already included in the form module and that's where you are using the activeCellRange variable.
Regarding the code in the worksheet module, there are a few areas that need to be modified:
-
In the Worksheet_Change event, you are currently checking if Target.Column = 2. This will only trigger the event if the change occurs in column B. If you want the event to trigger for changes in any column, you can remove the condition or modify it accordingly.
-
In the ShowTimeForm subroutine, you are currently setting activeCellRange = Target, but the Target variable is not used anywhere else in the subroutine. Instead, you should use the rng parameter that is passed to the subroutine. Modify the line to "Set activeCellRange = rng".
Here is the modified code:
Option Explicit Private activeCellRange As Range
-