In this string 'Node 2 Loop 2 1 2001 MAIN HALL LOBBY A1-5: Main Hall'
The first word and the number after like 'Node 2' is the name of a work sheet.
within the string, there is usally four numbers grouped together like '2001'
I want a VBA code that will do the following:
When I double click on a cell in column J from J15 downwards
If the cell value is not blank,
it will use the first word and the number to locate the worksheet,
then it will use the four numbers grouped together to find the cell within the worksheet that contains the same four number and select the cell.
Then pop up a message "Possible Location found"
Assistant
You can achieve this by using VBA in Excel. The following code snippet can be placed in the worksheet module that contains your data (e.g., Sheet1), or in the ThisWorkbook object if it needs to apply to multiple sheets. This code listens for a double-click event on cells in column J starting from J15 downward.
Here's a complete VBA code snippet you can use:
vba
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim fourDigitPattern As String
Dim foundCell As Range
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Use regex to find the four-digit number
fourDigitPattern = "\b\d{4}\b"
Set foundCell = Nothing
' Search for the four numbers in the specified worksheet
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value Like fourDigitPattern Then
If Len(cell.Value) = 4 Then
If IsNumeric(cell.Value) Then
If foundCell Is Nothing Then
Set foundCell = cell
End If
End If
End If
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
foundCell.Select
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Explanation of the Code:
Worksheet_BeforeDoubleClick Event: This event triggers when a cell is double-clicked. It checks if the double-click was on a cell in column J, starting from row 15.
Getting Cell Value: The value of the double-clicked cell is captured to use for processing.
Splitting the Cell Value: It splits the string to retrieve the first two parts, which are used as the name of the worksheet.
Locating the Worksheet: The code attempts to locate the specified worksheet in the workbook.
Finding the Four-Digit Number: It uses a loop to search through all cells within the used range of the located worksheet, checking for exact four-digit numeric values.
Selecting the Found Cell: If a matching four-digit number is found, that cell is selected, and a message box is displayed to the user.
Error Handling: If the worksheet is not found, or no matching number is found, appropriate message boxes alert the user.
How to Use the Code:
Open Excel and press ALT + F11 to open the VBA editor.
In the Project Explorer, find the worksheet where you want to apply this code (like Sheet1).
Double-click on the sheet name to open its code window.
Copy and paste the provided code into the code window.
Close the VBA editor and return to Excel.
Now, when you double-click on cells in column J from J15 downwards, the macro will execute based on the specified logic. Remember that double-clicking on blank cells will not activate the code.
User
The code is not finding the four digit number. To know if the code is working, can we have a popup that will show the worksheet name and a pop up that will show the fourdigit number. For each when I claick OK the code continues. This way, we can tell if it is using theright data to do the search
Assistant
Certainly! You can add message boxes to show the worksheet name and the four-digit number that the code is using to help you debug it. I've modified the original code to include these message boxes. Here's the updated version:
vba
User
It gets the write sheet name, but it is not able to isolate the four digit number
Assistant
To isolate the four-digit number correctly, we'll refine the logic in the code to explicitly extract four-digit numbers from the cell value. We will utilize a regular expression to search for four-digit numbers specifically. Here's how we can modify the code:
Use the Microsoft VBScript Regular Expressions library to help extract the four-digit number from the string.
Ensure that the regular expression matches only four-digit numbers.
Here’s the updated code:
vba
User
both finds are now correct but in the code, there is an error: Select method of Range class failed, on the line foundCell.Select
Assistant
The error "Select method of Range class failed" can occur when a sheet is not active or if you're trying to select a cell in a different worksheet from the one that is currently active. To avoid this issue, you can use the Application.Goto method instead of the Select method. This way, you will switch focus to the specified workbook and worksheet before selecting the cell.
Here's the updated code:
vba
User
Can you please write me a VBA code that will do the following: in the range A2:S19 if a cell is elected the Font size is increased to 16 points and returned back to 12 points when deselected
Assistant
Certainly! You can achieve this by using the Worksheet_SelectionChange event in VBA. This event triggers every time the user selects a different cell on the worksheet. The code will check if the selected cell is within the specified range (A2:S19) and will change the font size accordingly.
Here’s the VBA code to implement this functionality:
vba
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim selectedRange As Range
Set selectedRange = Me.Range("A2:S19")
' Check if the selected cell is within the specified range
If Not Intersect(Target, selectedRange) Is Nothing Then
' Increase font size for the selected cell(s)
Target.Font.Size = 16
End If
' Check if the previously selected cell(s) are outside of the specified range
If Not Intersect(Target, selectedRange) Is Nothing Then
' Reset font size for all other cells in the range to 12
selectedRange.Font.Size = 12
' Then, set the font size for currently selected cell(s) to 16
Target.Font.Size = 16
Else
' Reset font size for the entire range to 12 if the selection is outside the range
selectedRange.Font.Size = 12
End If
End Sub
How to Implement the Code:
Open Excel and press ALT + F11 to open the VBA editor.
In the Project Explorer, find the worksheet where you want to apply this functionality (for example, Sheet1).
Double-click on the sheet name to open its code window.
Copy and paste the provided code into the code window.
Close the VBA editor and return to Excel.
Explanation of the Code:
Worksheet_SelectionChange Event: This event is triggered whenever the user changes the selection within the worksheet.
Intersect Function: We use Intersect(Target, selectedRange) to check if the selected cell(s) fall within the range A2:S19.
Font Size Change: When a cell within the specified range is selected, its font size is set to 16 points. If any cell outside the range is selected, all cells in the specified range are set back to 12 points.
Now the functionality will work as you described: when you select a cell within A2:S19, its font size will increase to 16 points, and when you select a cell outside that range, the font size of the previously selected cells will revert to 12 points.
User
In the double click event below, when the cell is selected, can the sheet scroll to the left and to the top without loosing the selection: Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
code
Assistant
To scroll to the top-left corner of the Excel worksheet without losing the selection on the foundCell, you can adjust the ActiveWindow.ScrollRow and ActiveWindow.ScrollColumn properties after the selection change. This allows you to scroll the view while ensuring that the selected cell remains highlighted.
Here's how you can modify your existing double-click event code to include these adjustments:
vba
User
In the double click event below, is it possible to also write the value of the double clicked cell to the J1 of the sheet where a match is found. Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
code
Assistant
Yes, you can certainly modify the double-click event code to write the value of the double-clicked cell to cell J1 of the sheet where the match is found.
Here’s how you can modify your existing code to include that functionality:
vba
User
Can I have a vba code that will do the following: On worksheet deactivate, Clear the contents of J1 and restore all font size on the page to 10 points
Assistant
Certainly! You can achieve this by using the Worksheet_Deactivate event in VBA. This event is triggered whenever a user switches away from the specified worksheet. In this event, you can clear the contents of cell J1 and set the font size for all cells on the worksheet back to 10 points.
Here’s how to implement the code:
VBA Code
Open Excel and press ALT + F11 to open the VBA editor.
In the Project Explorer, find the worksheet where you want to apply this behavior (for example, Sheet1).
Double-click on the sheet name to open its code window.
Copy and paste the following code into the code window:
vba
Private Sub Worksheet_Deactivate()
' Clear the contents of J1
Me.Range("J1").ClearContents
' Set font size of all cells in the worksheet to 10 points
Me.Cells.Font.Size = 10
End Sub
Explanation of the Code:
Worksheet_Deactivate Event: This event will trigger when the user navigates away from the worksheet containing this code.
Me.Range("J1").ClearContents: This line clears the contents of cell J1 in the current worksheet.
Me.Cells.Font.Size = 10: This line sets the font size of all cells in the worksheet to 10 points.
Implementation:
After you've pasted the code, close the VBA editor.
You can now switch to another worksheet. When you do, the contents of cell J1 in the current worksheet will be cleared, and the font size of all cells will be reset to 10 points.
This code will effectively manage the contents of cell J1 and the font sizes as specified whenever you deactivate the worksheet.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim fourDigitPattern As String
Dim foundCell As Range
Dim fourDigitNumber As String
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Use regex to find the four-digit number
fourDigitPattern = "\b\d{4}\b"
Set foundCell = Nothing
' Search for the four numbers in the specified worksheet
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value Like fourDigitPattern Then
If Len(cell.Value) = 4 Then
If IsNumeric(cell.Value) Then
fourDigitNumber = cell.Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
If foundCell Is Nothing Then
Set foundCell = cell
End If
End If
End If
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
foundCell.Select
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Changes Made:
Message Box for Worksheet Name: After constructing the worksheet name, a message box will show which worksheet the code is attempting to search.
Message Box for Found Four-Digit Number: After discovering the four-digit number, another message box will pop up displaying the four-digit number that the code has found.
How to Test the Code:
Double-click on a cell in column J (from J15 down).
Respond to the message boxes that appear, which show the worksheet name and the found four-digit number.
This will help you confirm if the worksheet name and number are being identified correctly.
Make sure that the worksheets you are checking exist and that the four-digit numbers are properly formatted as numbers in the respective worksheets. This will enable you to trace the control flow more easily in your code.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
foundCell.Select
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Key Changes:
Regex Object: A new regex object is created to identify four-digit patterns.
Extracting Matches: The regex is executed on the cell value to find matches. If matches are found, the first matched four-digit number is stored in fourDigitNumber.
Searching Worksheet: After extracting the four-digit number, the code searches the specified worksheet for a cell containing this number and selects it if found.
How to Implement the Code:
Open Excel and press ALT + F11 to open the VBA editor.
In the Project Explorer, find the worksheet where you want to apply this code (e.g., Sheet1).
Double-click on the sheet name to open its code window.
Copy and paste the provided code into the code window.
Close the VBA editor and return to Excel.
Now, when you double-click on a cell in column J (from J15 down), a sequence of message boxes will appear showing the worksheet name, the four-digit number found, and whether its corresponding cell was located. This should help in debugging and understanding the flow of data.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
' Use Application.Goto to avoid select method error
Application.Goto foundCell, True
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Key Changes:
Using Application.Goto: Instead of foundCell.Select, we now use Application.Goto foundCell, True. This allows us to jump to the specified cell even if the worksheet is not currently active. The optional second parameter True will activate the sheet where the cell is located.
How to Implement the Updated Code:
Copy and paste this updated code into the same VBA editor location as before.
Close the VBA editor and return to Excel.
Now, when you double-click the cell in column J, it should correctly navigate to the found cell without throwing the select method error. If a matching cell is found, it will display the appropriate message and select the cell seamlessly.
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
' Use Application.Goto to avoid select method error
Application.Goto foundCell, True
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
' Use Application.Goto to avoid select method error
Application.Goto foundCell, True
' Scroll to the top-left corner without losing the foundCell selection
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Key Additions:
Scrolling with ActiveWindow.ScrollRow and ActiveWindow.ScrollColumn:
After using Application.Goto foundCell, True, the code now sets ActiveWindow.ScrollRow = 1 and ActiveWindow.ScrollColumn = 1 to scroll the view to the top-left corner of the worksheet.
How It Works:
The Application.Goto function ensures the foundCell is selected, and then the ActiveWindow.ScrollRow and ActiveWindow.ScrollColumn properties are used to move the scroll position of the Excel worksheet view to the start (cell A1) without deselecting the current selection.
Implementation:
Copy and paste the modified code into the same VBA editor location where your original code is stored.
Close the VBA editor and return to Excel.
This implementation will allow users to double-click cells in column J (from J15 down), and when the required conditions match, the sheet will scroll to the top-left corner while keeping the selection on the found cell.
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
' Use Application.Goto to avoid select method error
Application.Goto foundCell, True
' Scroll to the top-left corner without losing the foundCell selection
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
MsgBox "Possible Location found", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim cellValue As String
Dim splitValue As Variant
Dim foundCell As Range
Dim regex As Object
Dim matches As Object
Dim fourDigitNumber As String
' Check if the target cell is in column J and starting from row 15
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
' Get the value of the clicked cell in column J
cellValue = Target.Value
' Check if the cell is not blank
If cellValue <> "" Then
' Split the cell value to get the worksheet name (first word and number)
splitValue = Split(cellValue, " ")
If UBound(splitValue) >= 1 Then
' Construct the worksheet name
Dim worksheetName As String
worksheetName = splitValue(0) & " " & splitValue(1)
' Show the worksheet name in a popup
MsgBox "Worksheet to search: " & worksheetName, vbInformation
' Attempt to set the worksheet variable
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(worksheetName)
On Error GoTo 0
' Check if the worksheet was found
If Not ws Is Nothing Then
' Create a regex object to find four-digit numbers
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b\d{4}\b"
regex.Global = True
' Execute the regex on the cell value
Set matches = regex.Execute(cellValue)
' Check if we found any matches
If matches.Count > 0 Then
' Get the first matching four-digit number
fourDigitNumber = matches(0).Value
' Show found four-digit number in a popup
MsgBox "Found four-digit number: " & fourDigitNumber, vbInformation
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
Dim cell As Range
For Each cell In ws.UsedRange
If cell.Value = fourDigitNumber Then
Set foundCell = cell
Exit For ' Exit the loop after finding the first match
End If
Next cell
' If a match was found, select the cell and display a message
If Not foundCell Is Nothing Then
' Use Application.Goto to avoid select method error
Application.Goto foundCell, True
' Scroll to the top-left corner without losing the foundCell selection
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
' Write the value of the double-clicked cell into J1 of the found worksheet
ws.Range("J1").Value = cellValue
MsgBox "Possible Location found and value written to J1", vbInformation
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation
End If
End If
End If
End Sub
Key Changes:
Writing to J1: After successfully finding a match in the specified worksheet (ws), the code will assign the value of the double-clicked cell (cellValue) to ws.Range("J1").Value.
How It Works:
The code retains its original logic for finding and selecting the matching cell.
Once a match is found, it also writes the value from the double-clicked cell directly into cell J1 of the matched worksheet.
Implementation:
Copy and paste the modified code into the same VBA editor location where your original code is stored.
Close the VBA editor and return to Excel.
Now, when you double-click a cell in the specified range, the value of that cell will be written to J1 of the matched worksheet after the match is found. A message box will inform the user that a possible location was found and the value was written to J1.