Can you please check if the code I have written has any error and if it can still be…
- User
Can you please check if the code I have written has any error and if it can still be optimised: Private Sub Worksheet_Change(ByVal Target As Range) Dim rngI As Range Dim rngG As Range Dim cell As Range Dim innercell As Range
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("I6:I505")) Is Nothing Then If IsDate(Target.Value) Then If Target.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days.", vbInformation, "DATE" ActiveSheet.Range("G" & Target.Row).Select 'Exit Sub End If End If End If
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("G6:G505")) Is Nothing Then If Not doubleClickFlag Then ' DoublClickFlag previously set to True to prevent Change Reaction If Target.Value <> "" And Target.Offset(0, 2).Value = "" Then MsgBox "Date Completed not present", vbCritical, "DATE" Application.EnableEvents = False Target.Value = "" Application.EnableEvents = True End If End If Else MsgBox "Please change the Issue description ( column B ) as required.", vbInformation, "ISSUE" ActiveSheet.Range("B" & Target.Row).Select Else If Target.Value <> "" And Target.Offset(0, -5).Value <> "Service" Then MsgBox "Task is not a current Service", vbCritical, "TASK TYPE" Application.EnableEvents = False Target.Value = "" Application.EnableEvents = True End If End If Else If Target.Value <> "" And Target.Offset(0, -5).Value = "Service" And Target.Offset(0, 2).Value <> "" Then MsgBox "A New Service row will now be created", vbInformation, "NEW SERVICE" Target.Offset(0, -5).Value = "Serviced" Target.Offset(0, 1).Value = "" Dim newRow As Long newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 ActiveSheet.Range("B" & newRow).Value = "Service" ActiveSheet.Range("C" & newRow).Value = Target.Offset(0, 2).Value ActiveSheet.Range("F" & newRow).Value = Target.Offset(0, -1).Value ActiveSheet.Range("J" & newRow).Value = Target.Offset(0, 3).Value ActiveSheet.Range("L" & newRow).Value = Target.Offset(0, 5).Value ActiveSheet.Range("H" & newRow).Value = Target.Offset(0, 2).Value + Target.Value End If End If
If Target.Column = 5 Then ColumnList Target End If doubleClickFlag = False End Sub
- Assistant
- User
If IsDate(Target.Value) checks the value is a date. How do I check if the value is NOT a date
- Assistant
To check if the value is not a date, you can use the Not operator along with the IsDate function. Here's an example:
If Not IsDate(Target.Value) Then ' Code to execute if the value is not a date End IfIn your original code, you can replace the line
If IsDate(Target.Value) ThenwithIf Not IsDate(Target.Value) Thenin order to check if the value is not a date. - User
Rather than using; Target.Offset(0, 2).Value = "", I want to check if; Target.Offset(0, 2).Value = is not a date. How will I write it in VBA
- Assistant
To check if the value in
Target.Offset(0, 2).Valueis not a date, you can convert it to a date using theIsDatefunction and then use theNotoperator to check if it returnsFalse. Here's an example:If Not IsDate(Target.Offset(0, 2).Value) Then ' Code to execute if Target.Offset(0, 2).Value is not a date End IfIn your original code, you can replace the line
If Target.Offset(0, 2).Value = "" Thenwith the above code to check ifTarget.Offset(0, 2).Valueis not a date. - User
Are there errors in this VBA event: Private Sub Worksheet_Change(ByVal Target As Range) Dim rngI As Range Dim rngG As Range Dim cell As Range Dim innercell As Range
If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("I6:I505")) Is Nothing Then ' COLUMN I FOR SERVICE COMPLETED DATE If IsDate(Target.Value) And Target.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days.", vbInformation, "DATE" ActiveSheet.Range("G" & Target.Row).Select End If ElseIf IsDate(Target.Value) And Target.Offset(0, -7).Value <> "Service" Then MsgBox "Please change the Issue description ( column B ) as required.", vbInformation, "ISSUE" ActiveSheet.Range("B" & Target.Row).Select End If End If Exit Sub
- Assistant
- User
can VBA recognise the difference between a mouse click on a cell and a keyboard selection of a cell
- Assistant
No, VBA cannot directly recognize the difference between a mouse click on a cell and a keyboard selection of a cell. The Worksheet_Change event will trigger whenever a change is made to a cell, regardless of whether it was made using the mouse or the keyboard.
If you need to distinguish between a mouse click and keyboard selection, you would need to use a combination of different events and variables to track user actions. Here's an example approach:
-
Use the Worksheet_SelectionChange event to capture the initial selection when the user clicks on a cell.
-
Inside the Worksheet_SelectionChange event, store the current selection in a global variable.
-
Use the Worksheet_Change event to check if a change occurs after the initial selection is made.
-
Compare the current selection with the initial selection stored in the global variable to determine if the change was due to a keyboard action or a mouse click.
-
- User
In column I when I click on a cell I get a Message triggered by the Worksheet_SelectionChange event. If I enter a value into a cell in column I and press Enter on my keyboard to register the change I made in the cell, the selection immediately drops down to the cell below and triggers the Worksheet_SelectionChange event. How can I prevent this
- Assistant
To prevent the Worksheet_SelectionChange event from triggering when pressing Enter on the keyboard to register a change in a cell, you can use the Application.EnableEvents property to temporarily disable events while the change is being processed. Here's an example:
Dim initialSelection As Range Dim isEnterPressed As Boolean Private Sub Worksheet_SelectionChange(ByVal Target As Range) If isEnterPressed Then ' Reset the isEnterPressed flag and exit the event isEnterPressed = False Exit Sub End If Set initialSelection = Target End Sub Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Target, initialSelection) Is Nothing Then ' Change was due to a selection made with a keyboard MsgBox "Selected with keyboard" Else ' Change was due to a mouse click MsgBox "Clicked with mouse" End If End Sub Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' Set the isEnterPressed flag to indicate that a cell was changed with Enter key isEnterPressed = True End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' Disable events temporarily to avoid triggering Worksheet_Change event Application.EnableEvents = False ' Re-select the cell again to prevent moving down after Enter key Target.Select ' Enable events back Application.EnableEvents = True End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.