In the VBA module below, can you write me a module that can be called when 'MsgBox "Location found in row " & i & vbCrLf & "Row value in column G: " & ws.Cells(i, 7).Value & vbCrLf & vbCrLf & "Please head towards - " & ws.Cells(i, 4).Value & vbCrLf & vbCrLf & "Double Click column H to return to the Top of the Page", vbInformation, "ADDRESS FOUND"'
that will do the following:
in sheet 'Activations' in the next available row,
write the current date to & time to column A
and write 'ws.Range(ws.Cells(i, 1), ws.Cells(i, 7))' to column B
Sub SearchParameters()
Application.EnableEvents = False
Me.Unprotect Password:="edit"
Dim ws As Worksheet
Dim searchValue1 As String
Dim searchValue2 As String
Dim searchKeyword As String
Dim found As Boolean
Dim i As Long
Dim lastRow As Long
Dim instanceCount As Integer ' Initialize instanceCount variable
Dim matchRow As Long ' Track the row number for matches
code
Assistant
User
Assistant
User
I have placed 'LogToActivations' in module3. Do I have to write it like: ' Log the current date and time along with the clicked cell value to the Activations sheet
Call Moudle3:LogToActivations cellValue ' Call the logging subroutine
Assistant
User
In the VBA module below,
The line 'actSheet.Cells(nextRow, 2).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Value'
only copied part of the address found
Sub LogLocation(i As Long, ws As Worksheet)
Dim actSheet As Worksheet
Dim nextRow As Long
code
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
actSheet.Unprotect Password:="edit"
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Write the relevant cell range to column B of the next row
actSheet.Cells(nextRow, 2).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Value ' Make sure this works without issues
actSheet.Protect Password:="edit"
End Sub
Assistant
The line actSheet.Cells(nextRow, 2).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Value is copying the values from multiple cells into a single cell in the "Activations" sheet. When you try to assign a range of cells to a single cell, it will only take the value from the first cell of that range, hence it looks like it's only copying part of the address found.
User
I worked but it wrote the address twice in the same cell
Assistant
Set ws = ThisWorkbook.Sheets("List Format")
matchRow = 15 ' Start writing matches from row 15 in column J
' Clear all background colors in the range A2:G612
ws.Range("A2:G612").Interior.ColorIndex = xlNone
' Construct search values from H3, H5, and H7
searchValue1 = Trim(ws.Range("H3").Value) & " " & Trim(ws.Range("H5").Value)
searchValue2 = Trim(ws.Range("H7").Value)
searchKeyword = LCase(Trim(ws.Range("H9").Value))
' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
found = False
instanceCount = 0 ' Initialize instanceCount
' Loop through each row to search for the combination of H3 & H5 with H7
For i = 2 To lastRow
If ws.Cells(i, 1).Value = searchValue1 Then
If ws.Cells(i, 2).Value = searchValue2 Then
' If the combination of H3 & H5 with H7 is found
found = True
'MsgBox "Location found in row " & i & vbCrLf & "Row value in column G: " & ws.Cells(i, 7).Value & vbCrLf & "Double Click column H to return to the Top of the Page"
MsgBox "Location found in row " & i & vbCrLf & "Row value in column G: " & ws.Cells(i, 7).Value & vbCrLf & vbCrLf & "Please head towards - " & ws.Cells(i, 4).Value & vbCrLf & vbCrLf & "Double Click column H to return to the Top of the Page", vbInformation, "ADDRESS FOUND"
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Select
Exit For
End If
End If
Next i
' If the combination of H3 & H5 with H7 is not found, and H9 is found, restrict the search for H9 to the same rows where H3 & H5 are found
If Not found And Trim(ws.Range("H9").Value) <> "" Then
For i = 2 To lastRow
If InStr(1, LCase(ws.Cells(i, 3).Value), searchKeyword) > 0 And ws.Cells(i, 1).Value = searchValue1 Then
found = True
instanceCount = instanceCount + 1
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
'ws.Cells(matchRow, 10).Value = "Location found in row " & i & ", G: " & ws.Cells(i, 7).Value & ": " & ws.Cells(i, 4).Value
ws.Cells(matchRow, 10).Value = ws.Cells(i, 7).Value & ": " & ws.Cells(i, 4).Value
matchRow = matchRow + 1 ' Move to the next row in column J
End If
Next i
' Select the row if only one instance is found
If instanceCount = 1 Then
For i = 2 To lastRow
If InStr(1, LCase(ws.Cells(i, 3).Value), searchKeyword) > 0 And ws.Cells(i, 1).Value = searchValue1 Then
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Select
Exit For
End If
Next i
End If
End If
' If H3 & H5, H7, and H9 are not found together, highlight all rows with the value of H9 in column C
If Not found And Trim(ws.Range("H9").Value) <> "" Then
searchKeyword = LCase(ws.Range("H9").Value)
instanceCount = 0 ' Reset instanceCount before counting
For i = 2 To lastRow
If InStr(1, LCase(ws.Cells(i, 3).Value), searchKeyword) > 0 Then
found = True
instanceCount = instanceCount + 1
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
'ws.Cells(matchRow, 10).Value = "Location found in row " & i & ", G: " & ws.Cells(i, 7).Value & ": " & ws.Cells(i, 4).Value
ws.Cells(matchRow, 10).Value = ws.Cells(i, 7).Value & ": " & ws.Cells(i, 4).Value
matchRow = matchRow + 1 ' Move to the next row in column J
End If
Next i
' Select the row if only one instance is found
If instanceCount = 1 Then
For i = 2 To lastRow
If InStr(1, LCase(ws.Cells(i, 3).Value), searchKeyword) > 0 Then
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Select
Exit For
End If
Next i
End If
End If
' Check if any locations have been found
If Not found Then
MsgBox "Location Not Found" & vbCrLf & "Double Click column H to return to the Top of the Page", vbExclamation, "NO MATCH"
End If
Me.Protect Password:="edit"
Application.EnableEvents = True
End Sub
To create the requested module that writes the current date and time along with the specified row data to the "Activations" sheet when the message box is displayed, follow the steps below:
Create a new procedure that handles this action.
Insert the current date and time into column A of the next available row.
Copy the specified range from the originating worksheet to column B of the "Activations" sheet.
Here's how you can implement the module:
vba
Sub LogLocation(i As Long, ws As Worksheet)
Dim actSheet As Worksheet
Dim nextRow As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Write the relevant cell range to column B of the next row
actSheet.Cells(nextRow, 2).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Value ' Make sure this works without issues
End Sub
Update the SearchParameters Subroutine
Now, modify your existing SearchParameters subroutine to call LogLocation after the message box is displayed. Replace the MsgBox line as follows:
vba
If ws.Cells(i, 1).Value = searchValue1 Then
If ws.Cells(i, 2).Value = searchValue2 Then
' If the combination of H3 & H5 with H7 is found
found = True
' Call the LogLocation procedure to log the details
LogLocation i, ws
MsgBox "Location found in row " & i & vbCrLf & "Row value in column G: " & ws.Cells(i, 7).Value & vbCrLf & vbCrLf & "Please head towards - " & ws.Cells(i, 4).Value & vbCrLf & vbCrLf & "Double Click column H to return to the Top of the Page", vbInformation, "ADDRESS FOUND"
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Interior.Color = RGB(255, 255, 0) ' Highlight in yellow
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Select
Exit For
End If
End If
The LogLocation subroutine should now be responsible for logging the current date and time along with the specific range to the "Activations" sheet. Just ensure the "Activations" sheet exists in your workbook and the specified ranges in the LogLocation subroutine are valid.
Complete VBA Code Example
Below is the modified part of your SearchParameters procedure, including the addition of the LogLocation function:
vba
Sub SearchParameters()
Application.EnableEvents = False
Me.Unprotect Password:="edit"
Dim ws As Worksheet
Dim searchValue1 As String
Dim searchValue2 As String
Dim searchKeyword As String
Dim found As Boolean
Dim i As Long
Dim lastRow As Long
Dim instanceCount As Integer
Dim matchRow As Long
Set ws = ThisWorkbook.Sheets("List Format")
matchRow = 15
' Clear all background colors in the range A2:G612
ws.Range("A2:G612").Interior.ColorIndex = xlNone
searchValue1 = Trim(ws.Range("H3").Value) & " " & Trim(ws.Range("H5").Value)
searchValue2 = Trim(ws.Range("H7").Value)
searchKeyword = LCase(Trim(ws.Range("H9").Value))
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
found = False
instanceCount = 0
For i = 2 To lastRow
If ws.Cells(i, 1).Value = searchValue1 Then
If ws.Cells(i, 2).Value = searchValue2 Then
found = True
' Call the LogLocation procedure to log the details
LogLocation i, ws
MsgBox "Location found in row " & i & vbCrLf & "Row value in column G: " & ws.Cells(i, 7).Value & vbCrLf & vbCrLf & "Please head towards - " & ws.Cells(i, 4).Value & vbCrLf & vbCrLf & "Double Click column H to return to the Top of the Page", vbInformation, "ADDRESS FOUND"
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Interior.Color = RGB(255, 255, 0)
ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Select
Exit For
End If
End If
Next i
' Remaining part of the code...
Me.Protect Password:="edit"
Application.EnableEvents = True
End Sub
Sub LogLocation(i As Long, ws As Worksheet)
Dim actSheet As Worksheet
Dim nextRow As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Write the relevant cell range to column B of the next row
actSheet.Cells(nextRow, 2).Value = ws.Range(ws.Cells(i, 1), ws.Cells(i, 7)).Value
End Sub
This will create a full working solution to log the desired data to the "Activations" worksheet whenever a match is found.
In the VBA module below, can you write me a module that can be called when '' Get the value of the clicked cell in column J
cellValue = Target.Value'
that will do the following:
in sheet 'Activations' in the next available row,
write the current date to & time to column A
and write 'Target.Value' to column B
If Not Intersect(Target, Me.Range("J15:J" & Me.Rows.Count)) Is Nothing Then
' Prevent the default double-click action
Cancel = True
code
' 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, "SEARCH"
' 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 "Address: " & fourDigitNumber & " to be selected", vbInformation, "ADDRESS"
' 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
ws.Unprotect Password:="edit"
' Write the value of the double-clicked cell into J1 of the found worksheet
ws.Range("J1").Value = cellValue
ws.Protect Password:="edit"
MsgBox "Possible Location written Top of Page", vbInformation, "LOCATION"
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation, "NO MATCH"
End If
End If
End If
End Sub
Based on your request, we can create a separate subroutine called LogToActivations that will be responsible for logging the current date and time to column A and the value of the clicked cell in column B of the "Activations" sheet. Then, we will call this subroutine after the line where you get the value of the clicked cell in column J (cellValue = Target.Value).
Here’s how you can implement this:
Step 1: Create the Subroutine to Log Data
First, we will create the LogToActivations subroutine. This function will handle the task of writing the current date and time, as well as the clicked cell value, to the "Activations" sheet:
vba
Sub LogToActivations(clickedValue As String)
Dim actSheet As Worksheet
Dim nextRow As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Write the clicked cell value to column B of the next row
actSheet.Cells(nextRow, 2).Value = clickedValue
End Sub
Step 2: Integrate the Logging Functionality into Your Existing Code
Next, we need to modify your existing code to call the LogToActivations procedure right after retrieving the value of the clicked cell:
vba
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
' Log the current date and time along with the clicked cell value to the Activations sheet
LogToActivations cellValue ' Call the logging subroutine
' 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
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
' 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 "Address: " & fourDigitNumber & " to be selected", vbInformation, "ADDRESS"
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
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
Application.Goto foundCell, True
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
ws.Unprotect Password:="edit"
ws.Range("J1").Value = cellValue
ws.Protect Password:="edit"
MsgBox "Possible Location written Top of Page", vbInformation, "LOCATION"
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation, "NO MATCH"
End If
End If
End If
Complete VBA Code Example
Combining both parts, the complete code including the logging functionality would look like this:
vba
This complete code allows you to log the clicked cell value and the current date and time into the "Activations" sheet every time a cell in column J is double-clicked.
In VBA, to call a subroutine located in a different module, you don't need to specify the module name when calling it unless there's ambiguity or if the subroutine is not public. If your LogToActivations subroutine is declared as Public (which is the default for subroutines not explicitly defined as Private), you can simply call it without the module name.
How to Call a Subroutine from a Different Module
Given your LogToActivations subroutine is in Module3, you can call it as follows:
vba
Call Module3.LogToActivations(cellValue) ' Call the logging subroutine
However, since Call is optional in VBA, you can also use:
vba
Module3.LogToActivations cellValue ' Directly call the logging subroutine
Example Usage
In your worksheet's BeforeDoubleClick event, you can write:
vba
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
' Log the current date and time along with the clicked cell value to the Activations sheet
Call Module3.LogToActivations(cellValue) ' or simply Module3.LogToActivations cellValue
' Check if the cell is not blank
If cellValue <> "" Then
' ... (rest of your code)
End If
End If
Summary
Use Call Module3.LogToActivations(cellValue) or simply Module3.LogToActivations cellValue to call the subroutine from Module3.
Ensure that LogToActivations is defined as Public or is not specified as Private in Module3.
Here's a complete example of how your code could look in the BeforeDoubleClick event, assuming the LogToActivations subroutine is in Module3:
vba
This approach will properly log the clicked cell values into your "Activations" sheet and utilize the LogToActivations subroutine from Module3.
How to Concatenate the Values
To copy the values from columns A to G from the source worksheet into a single cell in the "Activations" sheet, you can concatenate the values into a single string. You could use a loop to join these values, or you could use the Join function to simplify this. Here’s how you can modify the LogLocation subroutine to do this:
vba
Sub LogLocation(i As Long, ws As Worksheet)
Dim actSheet As Worksheet
Dim nextRow As Long
Dim addressValues As String
Dim j As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
actSheet.Unprotect Password:="edit"
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Concatenate values from columns A to G into a single string
For j = 1 To 7 ' Loop through columns 1 (A) to 7 (G)
addressValues = addressValues & ws.Cells(i, j).Value & " " ' Append the cell value and a space
Next j
' Trim any trailing spaces
addressValues = Trim(addressValues)
' Write the concatenated string to column B of the next row
actSheet.Cells(nextRow, 2).Value = addressValues
actSheet.Protect Password:="edit"
End Sub
Key Changes
Loop to Concatenate Values: The For loop iterates through each column from 1 to 7 (representing A to G), concatenating the values into the addressValues string.
Trim: The Trim function is used to remove any trailing spaces after concatenation.
Single Assignment: Finally, the concatenated string addressValues is assigned to the corresponding cell in the "Activations" sheet.
Result
With these modifications, the entire range from columns A to G from the source worksheet will now be concatenated into a single string and stored in column B of the "Activations" sheet. This will give you a complete address record in that cell.
If the address is being written twice in the same cell, it's likely due to how you're building the concatenated string. This can occur if addressValues is not reset before you start concatenation for multiple calls of the LogLocation subroutine or if the subroutine is being called multiple times unnecessarily.
Ensure Proper Concatenation
To troubleshoot and resolve the issue, make sure to reinitialize the addressValues string at the beginning of the LogLocation subroutine. Here’s the revised code:
vba
Sub LogLocation(i As Long, ws As Worksheet)
Dim actSheet As Worksheet
Dim nextRow As Long
Dim addressValues As String
Dim j As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
actSheet.Unprotect Password:="edit"
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Initialize addressValues as an empty string
addressValues = ""
' Concatenate values from columns A to G into a single string
For j = 1 To 7 ' Loop through columns 1 (A) to 7 (G)
addressValues = addressValues & ws.Cells(i, j).Value & " " ' Append the cell value and a space
Next j
' Trim any trailing spaces
addressValues = Trim(addressValues)
' Write the concatenated string to column B of the next row
actSheet.Cells(nextRow, 2).Value = addressValues
actSheet.Protect Password:="edit"
End Sub
Key Points
Reinitialize addressValues: The addressValues string should be initialized as an empty string ("") at the beginning of the subroutine. This ensures that each time LogLocation is called, it starts with a fresh string for concatenation.
Additional Debugging
If you still see duplicates after these adjustments, consider checking where the LogLocation subroutine is called in your code. Make sure it's only being invoked once per event or condition that leads to writing to the "Activations" sheet.
If the subroutine is invoked correctly and the issue persists, you might want to add debugging output (like Debug.Print) to see what's being passed to LogLocation. For example:
vba
Debug.Print "Logging address for row: " & i
This can help track if LogLocation is called multiple times with the same or different i values. This way, you'll have traces in the Immediate Window of the VBA editor, showing when and how often the function is being called.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim cellValue As String
Dim splitValue As Variant
Dim worksheetName As String
Dim ws As Worksheet
Dim regex As Object
Dim matches As Object
Dim foundCell As Range
Dim fourDigitNumber As String
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
' Log the current date and time along with the clicked cell value to the Activations sheet
LogToActivations cellValue ' Call the logging subroutine
' 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
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
' 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 "Address: " & fourDigitNumber & " to be selected", vbInformation, "ADDRESS"
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
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
Application.Goto foundCell, True
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
ws.Unprotect Password:="edit"
ws.Range("J1").Value = cellValue
ws.Protect Password:="edit"
MsgBox "Possible Location written Top of Page", vbInformation, "LOCATION"
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation, "NO MATCH"
End If
End If
End If
End Sub
Sub LogToActivations(clickedValue As String)
Dim actSheet As Worksheet
Dim nextRow As Long
' Set the reference to the "Activations" sheet
Set actSheet = ThisWorkbook.Sheets("Activations")
' Find the next available row in the Activations sheet
nextRow = actSheet.Cells(actSheet.Rows.Count, 1).End(xlUp).Row + 1
' Write current date and time to column A of the next row
actSheet.Cells(nextRow, 1).Value = Now
' Write the clicked cell value to column B of the next row
actSheet.Cells(nextRow, 2).Value = clickedValue
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim cellValue As String
Dim splitValue As Variant
Dim worksheetName As String
Dim ws As Worksheet
Dim regex As Object
Dim matches As Object
Dim foundCell As Range
Dim fourDigitNumber As String
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
' Log the current date and time along with the clicked cell value to the Activations sheet
Call Module3.LogToActivations(cellValue) ' or Module3.LogToActivations cellValue
' 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
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
' 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 "Address: " & fourDigitNumber & " to be selected", vbInformation, "ADDRESS"
' Now search for the four-digit number in the specified worksheet
Set foundCell = Nothing
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
Application.Goto foundCell, True
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
ws.Unprotect Password:="edit"
ws.Range("J1").Value = cellValue
ws.Protect Password:="edit"
MsgBox "Possible Location written Top of Page", vbInformation, "LOCATION"
Else
MsgBox "No matching four-digit number found in the worksheet.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "No four-digit number found in the cell value.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Worksheet '" & worksheetName & "' not found.", vbExclamation, "NO MATCH"
End If
Else
MsgBox "Invalid cell format. Ensure it contains the correct information.", vbExclamation, "NO MATCH"
End If
End If
End If
End Sub