When I click OK on the Form Button, it will occassionaly place the values in the right…
- User
When I click OK on the Form Button, it will occassionaly place the values in the right location. activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 29).Value = EndTime.Value However most of the time, it does not put the values in the right location. I also noticed that when I put values into the text boxes in the form and click OK, the form closes, but when I activate the form again the previous values entered in the text boxes are still there. Is there a variable memory issue that needs to be cleared that may also be affecting the correct location placement of values. Below is the Form code.
Private activeCellRange As Range
Private Sub UserForm_Initialize() ' Assign the initial 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(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' Update the active cell whenever the user selects a different cell Set activeCellRange = Target End Sub
- Assistant
There are a few potential issues that could be causing the problem you described.
-
Make sure that the
Initializeevent is being triggered correctly when the form is opened. This event should set theactiveCellRangevariable to the currently active cell. -
Check that the
Offsetvalues are correct for your specific situation. The values0, 28and0, 29assume that you are offsetting by 28 columns to the right for the start time and 29 columns to the right for the end time. Verify that these values are correct for your sheet. -
Double-check that the
StartTimeandEndTimecontrols are correctly linked to the text boxes on your form. Any values entered into these text boxes should be stored in those control variables. -
Verify that your form is not being closed prematurely or reopened without resetting the
activeCellRange. TheQueryCloseevent should prevent the form from being closed unintentionally, but make sure that you are properly hiding the form and resetting the variable when needed.
-
- User
Please find below the SHEET CODE that activates the Form 'TimeForm1' and the FORM CODE.
SHEET 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 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 activeCellRange As Range Set activeCellRange = rng TimeForm1.Show vbModal End Sub
FORM CODE: Private activeCellRange As Range
Private Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub
Private Sub StartEndButton1_Click() activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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
Thank you for providing your code. After reviewing it, I noticed a potential issue with your use of the
Worksheet_Changeevent in the sheet code.In your
Worksheet_Changeevent, you are checking if the changed cell is in column 2 (Target.Column = 2) and if the value is numeric (IsNumeric(Target.Value)). If these conditions are met, you call theShowTimeFormsubroutine.The problem is that when the
ShowTimeFormsubroutine is called, it sets theactiveCellRangeto the changed cell (Set activeCellRange = rng). However, since the cell's value has changed and triggered theWorksheet_Changeevent, it is not the active cell at the moment theShowTimeFormsubroutine is called.To fix this, you can use the
Application.Callerproperty to get the address of the cell that triggered the event. Replace theShowTimeFormsubroutine with the modified code shown below:Sub ShowTimeForm(ByVal rng As Range) Dim activeCellRange As Range Set activeCellRange = Me.Range(Application.Caller) TimeForm1.Show vbModal End Sub - User
With the change, I am getting an error 'Method 'Range' of object '_Worsheet' failed on the code line: 'Set activeCellRange = Me.Range(Application.Caller)'
- Assistant
Apologies for the confusion. Since you're calling the
ShowTimeFormsubroutine from the worksheet's code module, usingMe.Range(Application.Caller)will not work correctly.Instead, you can pass the address of the changed cell as a string to the
ShowTimeFormsubroutine. Modify theWorksheet_Changeevent and theShowTimeFormsubroutine as follows:SHEET CODE:
Option Explicit 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) Set TimeForm1.activeCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub - User
I am getting error: 'Method or data member not found' on line '.activeCellRange =' in the event : Sub ShowTimeForm(ByVal rngAddress As String) Set TimeForm1.activeCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub
- Assistant
I apologize for the confusion. The
activeCellRangevariable is declared as private in the form code, so it cannot be accessed directly from the sheet code.To resolve this issue, we can modify the form code to include a property that allows the sheet code to set the
activeCellRange. Here's the updated code:FORM CODE:
Private activeCellRange As Range Public Property Let ActiveCellRange(rng As Range) Set activeCellRange = rng End Property Public Property Get ActiveCellRange() As Range Set ActiveCellRange = activeCellRange End Property Private Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub Private Sub StartEndButton1_Click() activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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 - User
I am getting an error 'Ambiguous name detected: activeCellRange, in the Form code page
- Assistant
Apologies for the confusion. The error is caused by having duplicate declarations of the
activeCellRangevariable in both the form code and the sheet code.To fix this issue, you can remove the declaration of
activeCellRangefrom the form code because it's already declared as a property.Here's the updated form code:
FORM CODE:
Private Sub UserForm_Initialize() ActiveCellRange = ActiveCell End Sub Private Sub StartEndButton1_Click() ActiveCellRange.Offset(0, 28).Value = StartTime.Value ActiveCellRange.Offset(0, 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) ActiveCellRange = Target End SubRemove the
Private activeCellRange As Rangeline from the form code page.Ensure that the property is still defined in the form code.
By making this change, you should be able to resolve the "Ambiguous name detected" error.
Let me know if you have any further questions!
- User
Here is what I currently have in the Form Code Page. Is this correct or should any part of it be removed because I noticed that your last Form Code recommendation is slightly different:
Public Property Let activeCellRange(rng As Range) Set activeCellRange = rng End PropertyPublic Property Get activeCellRange() As Range Set activeCellRange = activeCellRange End Property
Private Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub
Private Sub StartEndButton1_Click() activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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
Apologies for the confusion. It appears that you have declared the
activeCellRangeproperty correctly in your form code.Please ignore my previous suggestion to remove the property declaration.
The code you provided in the form code section seems correct, so you can keep it as-is:
Public Property Let activeCellRange(rng As Range) Set activeCellRange = rng End Property Public Property Get activeCellRange() As Range Set activeCellRange = activeCellRange End Property Private Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub Private Sub StartEndButton1_Click() activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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 SubOnly the
activeCellRangeproperty declaration part should be modified. TheUserForm_Initialize, , , and procedures should remain the same. - User
I am getting an error 'Invalid use of property' on ' activeCellRange = ActiveCell ' in the Private Sub UserForm_Initialize() event
- Assistant
I apologize for the confusion. The issue is that you are using the same name for both the property and the local variable in the
UserForm_Initializeevent, which is causing the error.To resolve this, you need to use a different name for the local variable in the
UserForm_Initializeevent. You can rename it toacRangeor any other suitable name.Here's the updated code for the
UserForm_Initializeevent:Private Sub UserForm_Initialize() Dim acRange As Range Set acRange = ActiveCell activeCellRange = acRange End SubBy using a different name for the local variable, you avoid the conflict with the property name. This should resolve the "Invalid use of property" error.
Please make sure the modifications are in place for the
UserForm_Initializeevent, and let me know if you still encounter any issues. - User
I am now getting an error 'Invalid use of property' on the line 'Set activeCellRange = rng' in the 'Public Property Let activeCellRange( rng As Range) event
- Assistant
I apologize for the confusion once again. The error is occurring because you're assigning the same name
activeCellRangeto both the property and the local variable in thePublic Property Let activeCellRangeevent, causing a conflict.To resolve this issue, you should rename the local variable in the
Public Property Let activeCellRangeevent. For example, you can change it tonewActiveCellRange.Here's the updated code for the
Public Property Let activeCellRangeevent:Public Property Let activeCellRange(rng As Range) Set newActiveCellRange = rng End Property Private Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub Private Sub StartEndButton1_Click() activeCellRange.Offset(0, 28).Value = StartTime.Value activeCellRange.Offset(0, 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 - User
error 'Invalid use of property' on 'activeCellRange' in "Public Sub UserForm_Initialize() Set activeCellRange = ActiveCell End Sub"
- Assistant
Apologies for the confusion once again. The error is occurring because you are attempting to directly assign a value to the
activeCellRangeproperty, which is not allowed since it is a property with aProperty GetandProperty Letpair.To resolve this, you need to modify the code in the
UserForm_Initializeevent to assign the value to the_activeCellRangevariable within the form, instead of directly using the property name.Here's the updated code for the
UserForm_Initializeevent:Private Sub UserForm_Initialize() Me.activeCellRange = ActiveCell End SubBy using
Me.activeCellRange, you are accessing the property within the form and correctly assigning the value to the underlying variable.Please make sure the modifications are in place for the
UserForm_Initializeevent and let me know if you still encounter any issues. - User
I am getting an error 'Invalid use of property' on the line '.activeCellRange =' in the event 'Sub ShowTimeForm(ByVal rngAddress As String) Set TimeForm1.activeCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End Sub' which is the Sheet Page
- Assistant
Apologies for the confusion. The error is occurring because
activeCellRangeis a property within the form, so it needs to be accessed using the form object (TimeForm1) instead of theUserFormkeyword.To resolve this, you need to modify the code in the
ShowTimeFormsubroutine to set theactiveCellRangeproperty within the form.Here's the updated code for the
ShowTimeFormsubroutine:Sub ShowTimeForm(ByVal rngAddress As String) TimeForm1.ActiveCellRange = Me.Range(rngAddress) TimeForm1.Show vbModal End SubBy using
TimeForm1.ActiveCellRange, you are accessing the property within the form and correctly setting the value.Please make sure the modifications are in place for the
ShowTimeFormsubroutine, and let me know if you still encounter any