The following code is not getting the values from the sSuppliers sheet: ’ Check if…
- User
The following code is not getting the values from the sSuppliers sheet: ’ Check if double-clicked cell is in column H (8) of sheet ‘ITEMPR’ and is below row 1 If Target.Column = 8 And Target.Worksheet.Name = “ITEMPR” And Target.Row > 1 Then Application.EnableEvents = False
’ Get the value from column A in the same row Dim searchValue As String searchValue = Target.Offset(0, -7).Value ’ Find matching rows in sheet ‘Suppliers’ Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Worksheets(“Suppliers”) Dim foundRange As Range Set foundRange = suppliersSheet.Columns(“A:A”).Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) ’ Copy matching rows to clipboard Dim clipboardData As Object Set clipboardData = GetClipboard() If Not foundRange Is Nothing Then Dim firstFoundRange As Range Set firstFoundRange = foundRange ’ Store the first found range Dim rowValues As String Do rowValues = rowValues & Join(Application.Transpose(foundRange.EntireRow.Columns(“A:F”).Value), vbTab) & vbCrLf Set foundRange = suppliersSheet.Columns(“A:A”).FindNext(foundRange) Loop While Not foundRange Is Nothing And foundRange.Address <> firstFoundRange.Address clipboardData.SetText rowValues clipboardData.PutInClipboard ’ Open Notepad and paste the clipboard contents Shell “notepad.exe”, vbNormalFocus Application.Wait Now + TimeValue(“00:00:01”) SendKeys “^v” End If Application.EnableEvents = TrueEnd If Cancel = True ’ Prevents entering edit mode on double-click
Function GetClipboard() As Object Dim clipboardData As Object Set clipboardData = CreateObject(“New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}”) Set GetClipboard = clipboardData End Function
- Assistant
The code provided seems to have some syntax errors and incorrect logic. Here is the corrected code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' Check if double-clicked cell is in column H (8) of sheet ITEMPR and is below row 1 If Target.Column = 8 And Target.Worksheet.Name = "ITEMPR" And Target.Row > 1 Then Application.EnableEvents = False ' Get the value from column A in the same row Dim searchValue As String searchValue = Target.Offset(0, -7).Value ' Find matching rows in sheet Suppliers Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Worksheets("Suppliers") Dim foundRange As Range Set foundRange = suppliersSheet.Columns("A:A").Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) ' Copy matching rows to clipboard Dim clipboardData As Object Set clipboardData = GetClipboard() If Not foundRange Is Nothing Then Dim firstFoundRange As Range Set firstFoundRange = foundRange ' Store the first found range Dim rowValues As String Do rowValues = rowValues & Join(Application.Transpose(foundRange.EntireRow.Columns("A:F").Value), vbTab) & vbCrLf Set foundRange = suppliersSheet.Columns("A:A").FindNext(foundRange) Loop While Not foundRange Is Nothing And foundRange.Address <> firstFoundRange.Address clipboardData.SetText rowValues clipboardData.PutInClipboard ' Open Notepad and paste the clipboard contents Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys "^v" End If Application.EnableEvents = True End If Cancel = True ' Prevent entering edit mode on double-click End Sub Function GetClipboard() As Object Dim clipboardData As Object Set clipboardData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") Set GetClipboard = clipboardData End Function - User
Can you try a different approach. There are matching values in both sheets but the code method is not working
- Assistant
If the code is not working to find matching values in both sheets, you can try a different approach using a loop to iterate through the cells in column A of the "Suppliers" sheet and compare them with the search value. Here's an example of how you can modify the code:
- User
It is still not working. What about a different method where it opens Note Pad and copies the maches found one at a time.
- Assistant
- User
No, It did not work. What about trying a completely different VBA approach
- Assistant
Apologies for the confusion. Here's another VBA approach that searches for matching values in both sheets and copies the results to a new worksheet, which can then be copied to the clipboard:
- User
Yes it copied it to a Reults sheet. But I want only the values and the formatting. But this method defeats my objective because I wanted to open the results in Note Pad
- Assistant
Apologies for the confusion. If you want to copy only the values and formatting to Notepad, you can modify the code as follows:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' Check if double-clicked cell is in column H (8) of sheet ITEMPR and is below row 1 If Target.Column = 8 And Target.Worksheet.Name = "ITEMPR" And Target.Row > 1 Then Application.EnableEvents = False ' Get the value from column A in the same row Dim searchValue As String searchValue = Target.Offset(0, -7).Value ' Find matching rows in sheet Suppliers Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Worksheets("Suppliers") Dim suppliersRange As Range Set suppliersRange = suppliersSheet.Columns("A:A") Dim clipboardData As Object Set clipboardData = GetClipboard() ' Clear the clipboard clipboardData.SetText "" clipboardData.PutInClipboard For Each supplierCell In suppliersRange.Cells If supplierCell.Value = searchValue Then ' Copy matching row's values and formatting to clipboard supplierCell.EntireRow.Copy ' Wait for clipboard to update Application.Wait Now + TimeValue("00:00:01") ' Copy values and formatting to Notepad using SendKeys AppActivate "Notepad" SendKeys "^v" ' Wait for clipboard to clear Application.Wait Now + TimeValue("00:00:01") End If Next supplierCell ' Activate the original sheet Me.Activate Application.EnableEvents = True End If Cancel = True ' Prevent entering edit mode on double-click End Sub Function GetClipboard() As Object Dim clipboardData As Object Set clipboardData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") Set GetClipboard = clipboardData End Function - User
Can you correct this VBA code: If Target.Column = 8 And Target.Worksheet.Name = "ITEMPR" And Target.row > 1 Then Application.EnableEvents = False
' Get the value from column A in the same row Dim searchValue As String searchValue = Target.Offset(0, -7).value ' Find matching rows in sheet Suppliers Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Worksheets("Suppliers") Dim suppliersRange As Range Set suppliersRange = suppliersSheet.Columns("A:A") Dim supplierCell As Range Dim resultRow As Long resultRow = 1Dim outputString As String
For Each supplierCell In suppliersRange.Cells If supplierCell.value = searchValue Then outputString = supplierCell.EntireRow.Copy resultsSheet.Cells(resultRow, 1) resultRow = resultRow + 1 End If Next supplierCell Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys outputString Cancel = True ' Prevent entering edit mode on double-click Application.EnableEvents = True End IfEnd Sub
- Assistant
Certainly! Here's the corrected code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' Check if double-clicked cell is in column H (8) of sheet ITEMPR and is below row 1 If Target.Column = 8 And Target.Worksheet.Name = "ITEMPR" And Target.Row > 1 Then Application.EnableEvents = False ' Get the value from column A in the same row Dim searchValue As String searchValue = Target.Offset(0, -7).Value ' Find matching rows in sheet Suppliers Dim suppliersSheet As Worksheet Set suppliersSheet = ThisWorkbook.Worksheets("Suppliers") Dim suppliersRange As Range Set suppliersRange = suppliersSheet.Columns("A:A") Dim supplierCell As Range Dim resultRow As Long resultRow = 1 Dim outputString As String outputString = "" For Each supplierCell In suppliersRange.Cells If supplierCell.Value = searchValue Then ' Append the values from the row to the output string outputString = outputString & Join(Application.Transpose(supplierCell.EntireRow.Value), vbTab) & vbCrLf resultRow = resultRow + 1 End If Next supplierCell ' Open Notepad and paste the output string Shell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") SendKeys outputString Cancel = True ' Prevent entering edit mode on double-click Application.EnableEvents = True End If End Sub