Can the code below be improved for efficiency, speed and stability; Private Sub…
- User
Can the code below be improved for efficiency, speed and stability;
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'If Target.Cells.Count > 1 Then Exit Sub 'If Target.Count > 1 Then Exit Sub If Target.Count > 1 Or IsNumeric(Target.Value) Then Exit Sub Dim FormulaReference As String FormulaReference = Target.Formula If Left(FormulaReference, 1) = "=" Then ' Remove leading "=" and single apostrophes around the worksheet name: FormulaReference = Mid(FormulaReference, 2) ' Remove leading "=" FormulaReference = Replace(FormulaReference, "'", "") ' Remove single apostrophes ' Extract worksheet name and cell address using Split: Dim FormulaParts As Variant FormulaParts = Split(FormulaReference, "!") If UBound(FormulaParts) >= 1 Then Dim WorksheetName As String, CellAddress As String WorksheetName = FormulaParts(0) CellAddress = Right(FormulaReference, Len(FormulaReference) - Len(WorksheetName) - 1) Dim DestinationWorksheet As Worksheet Set DestinationWorksheet = ThisWorkbook.Worksheets(WorksheetName) If Not DestinationWorksheet Is Nothing Then DestinationWorksheet.Activate DestinationWorksheet.Range(CellAddress).Select End If End If End IfCancel = True End Sub
- Assistant
Yes, there are several ways this code can be improved for efficiency, speed, and stability:
-
Instead of using
Target.Count > 1to check if the range has multiple cells, useTarget.Cells.Count > 1. This will ensure that the code only runs if the target range has multiple cells. -
Instead of checking if
IsNumeric(Target.Value), useIsError(Target.Value). This is more efficient and will catch all potential error values in the cell. -
Instead of using string manipulation functions like
Left,Mid,Right, andLen, use the built-inRange.Addressproperty to get the cell address directly. This is more efficient and will simplify the code. -
Instead of activating the destination worksheet before selecting the cell, directly select the cell on the destination worksheet without activating it. This is faster and more efficient.
-
Error handling should be added to handle cases where the destination worksheet or cell address is invalid.
With these improvements, the code will be more efficient, faster, and more stable.
-
- User
If the code below be improved for efficiency, speed and stability can you please re-write it for me;
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'If Target.Cells.Count > 1 Then Exit Sub 'If Target.Count > 1 Then Exit Sub If Target.Count > 1 Or IsNumeric(Target.Value) Then Exit Sub Dim FormulaReference As String FormulaReference = Target.Formula If Left(FormulaReference, 1) = "=" Then ' Remove leading "=" and single apostrophes around the worksheet name: FormulaReference = Mid(FormulaReference, 2) ' Remove leading "=" FormulaReference = Replace(FormulaReference, "'", "") ' Remove single apostrophes ' Extract worksheet name and cell address using Split: Dim FormulaParts As Variant FormulaParts = Split(FormulaReference, "!") If UBound(FormulaParts) >= 1 Then Dim WorksheetName As String, CellAddress As String WorksheetName = FormulaParts(0) CellAddress = Right(FormulaReference, Len(FormulaReference) - Len(WorksheetName) - 1) Dim DestinationWorksheet As Worksheet Set DestinationWorksheet = ThisWorkbook.Worksheets(WorksheetName) If Not DestinationWorksheet Is Nothing Then DestinationWorksheet.Activate DestinationWorksheet.Range(CellAddress).Select End If End If End If - Assistant
Here is the rewritten version of the code with improvements for efficiency, speed, and stability:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Cells.Count <> 1 Then Exit Sub If Not IsError(Target.Value) Then Exit Sub Dim FormulaReference As String FormulaReference = Target.Formula If Left(FormulaReference, 1) = "=" Then FormulaReference = Replace(FormulaReference, "=", "") ' Remove leading "=" and single apostrophes FormulaReference = Replace(FormulaReference, "'", "") Dim FormulaParts As Variant FormulaParts = Split(FormulaReference, "!") If UBound(FormulaParts) >= 1 Then Dim WorksheetName As String, CellAddress As String WorksheetName = FormulaParts(0) CellAddress = FormulaParts(1) Dim DestinationWorksheet As Worksheet On Error Resume Next Set DestinationWorksheet = ThisWorkbook.Worksheets(WorksheetName) On Error GoTo 0 If Not DestinationWorksheet Is Nothing Then Dim rng As Range Set rng = DestinationWorksheet.Range(CellAddress) If Not rng Is Nothing Then Application.Goto rng Cancel = True End If End If End If End If End Sub - User
When I double click on a cell, because it is locked, it warns me that the cell is protected. In the code that you improved, can you disable all warnings
- Assistant
To disable all warnings, including the warning that the cell is protected when double-clicking on a locked cell, you can use the
Application.DisplayAlertsproperty. Here is the updated version of the code with warnings disabled:Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Application.DisplayAlerts = False ' Disable all alerts If Target.Cells.Count <> 1 Then Exit Sub If Not IsError(Target.Value) Then Exit Sub Dim FormulaReference As String FormulaReference = Target.Formula If Left(FormulaReference, 1) = "=" Then FormulaReference = Replace(FormulaReference, "=", "") ' Remove leading "=" and single apostrophes FormulaReference = Replace(FormulaReference, "'", "") Dim FormulaParts As Variant FormulaParts = Split(FormulaReference, "!") If UBound(FormulaParts) >= 1 Then Dim WorksheetName As String, CellAddress As String WorksheetName = FormulaParts(0) CellAddress = FormulaParts(1) Dim DestinationWorksheet As Worksheet On Error Resume Next Set DestinationWorksheet = ThisWorkbook.Worksheets(WorksheetName) On Error GoTo 0 If Not DestinationWorksheet Is Nothing Then Dim rng As Range Set rng = DestinationWorksheet.Range(CellAddress) If Not rng Is Nothing Then Application.Goto rng Cancel = True End If End If End If End If Application.DisplayAlerts = True ' Re-enable alerts End Sub - User
It still displays alerts
- Assistant
If the code above is still displaying alerts, you can try using the
Application.EnableEventsproperty to temporarily disable event handling in Excel. Here is the updated code withApplication.EnableEventsset toFalsebefore the code execution and set back toTrueafter the code execution:Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Application.EnableEvents = False ' Disable event handling Application.DisplayAlerts = False ' Disable all alerts If Target.Cells.Count <> 1 Then Exit Sub If Not IsError(Target.Value) Then Exit Sub Dim FormulaReference As String FormulaReference = Target.Formula If Left(FormulaReference, 1) = "=" Then FormulaReference = Replace(FormulaReference, "=", "") ' Remove leading "=" and single apostrophes FormulaReference = Replace(FormulaReference, "'", "") Dim FormulaParts As Variant FormulaParts = Split(FormulaReference, "!") If UBound(FormulaParts) >= 1 Then Dim WorksheetName As String, CellAddress As String WorksheetName = FormulaParts(0) CellAddress = FormulaParts(1) Dim DestinationWorksheet As Worksheet On Error Resume Next Set DestinationWorksheet = ThisWorkbook.Worksheets(WorksheetName) On Error GoTo 0 If Not DestinationWorksheet Is Nothing Then Dim rng As Range Set rng = DestinationWorksheet.Range(CellAddress) If Not rng Is Nothing Then Application.Goto rng Cancel = True End If End If End If End If Application.DisplayAlerts = True ' Re-enable alerts Application.EnableEvents = True ' Re-enable event handling End Sub - User
Still displaying allerts
- Assistant
If the alerts are still showing even after disabling
Application.DisplayAlertsandApplication.EnableEvents, you can try temporarily unprotecting the worksheet before executing the code and then protecting it back afterwards. Here is the updated code: - User
If the code below be improved for efficiency, speed and stability can you please re-write it for me;
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Check if the selected cell is within the specified range If Not Intersect(Target, Range("A2:T60")) Is Nothing Then ' Exit if multiple cells are selected or if the target value is a number If Target.Count > 1 Or IsNumeric(Target.Value) Then Exit Sub ' Reset font size of previously selected cell (if applicable) Me.Unprotect Password:="edit" ' Unlock the sheet Application.EnableEvents = False If SelectedCellAddress <> "" Then
With Range(SelectedCellAddress) .Font.Size = 8 End With End If ' Store address of newly selected cell and set font size to 12 SelectedCellAddress = Target.Address With Target .Font.Size = 12 End WithEnd If Application.EnableEvents = True Me.Protect Password:="edit" ' Lock the sheet
End Sub
- Assistant
Here is a rewritten version of the code with improvements for efficiency, speed, and stability:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim ws As Worksheet Set ws = ThisWorkbook.ActiveSheet ' Check if the selected cell is within the specified range If Intersect(Target, ws.Range("A2:T60")) Is Nothing Then Exit Sub ' Temporarily unprotect the sheet ws.Unprotect Password:="edit" ' Exit if multiple cells are selected or if the target value is a number If Target.Cells.Count <> 1 Or IsNumeric(Target.Value) Then ' Restore font size of previously selected cell (if applicable) Dim previousCell As Range Set previousCell = ws.Range(SelectedCellAddress) If previousCell IsNot Nothing Then previousCell.Font.Size = 8 End If ' Clear the selected cell address SelectedCellAddress = "" Else ' Restore font size of previously selected cell (if applicable) If SelectedCellAddress <> "" Then Dim previousCell As Range Set previousCell = ws.Range(SelectedCellAddress) If previousCell IsNot Nothing Then previousCell.Font.Size = 8 End If End If ' Set font size of newly selected cell to 12 Target.Font.Size = 12 ' Store the address of newly selected cell SelectedCellAddress = Target.Address End If ' Re-protect the sheet ws.Protect Password:="edit" End Sub