Copy/Paste Values of Results of FILTER function

10sGP

New Member
Joined
May 28, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
First post on here.

I'm looking to double click one cell from a FILTER function results and get that row copied to another workbook.
For example, let's say that
Excel Formula:
[B]=FILTER(A5:D20,C5:C20=H2,"")[/B]
gives me 10 results. I want to double click Cell A of one of those 10 results and get the whole row copied to another workbook.
While searching, I came across the code shown below by Tom Urtis. With very little modification, it does what I need, but not for FILTER function results. I just learned you cannot just copy/paste FILTER function results, you have to copy/PASTE VALUES. I tried this:
VBA Code:
.PasteSpecial Paste:=xlPasteValues
but that gives me an error.
Any ideas on how to accomplish this?

Thank you.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub
Cancel = True
With Sheets("Sheet3")
Target.EntireRow.Copy .Range("A" & .Cells(Rows.Count, 1).End(xlUp).row + 1)
End With
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
This may or may not be an acceptable workaround for your problem. I don't know what your data looks like, or whether you really want to copy the entire row (or just columns A-D) so I've assumed for the purpose of this demonstration that you just want to copy the first 4 columns.

With a data set that starts like this:
Book1
ABCDEFGH
1
2123
3
4HDR1HDR2HDR3HDR4
5Row 5 Col 1Row 5 Col 2123Row 5 Col 4
6Row 6 Col 1Row 6 Col 2123Row 6 Col 4
7Row 7 Col 1Row 7 Col 2xRow 7 Col 4
8Row 8 Col 1Row 8 Col 2xRow 8 Col 4
9Row 9 Col 1Row 9 Col 2123Row 9 Col 4
10Row 10 Col 1Row 10 Col 2123Row 10 Col 4
11Row 11 Col 1Row 11 Col 2123Row 11 Col 4
12Row 12 Col 1Row 12 Col 2123Row 12 Col 4
13Row 13 Col 1Row 13 Col 2123Row 13 Col 4
14Row 14 Col 1Row 14 Col 2xRow 14 Col 4
15Row 15 Col 1Row 15 Col 2xRow 15 Col 4
16Row 16 Col 1Row 16 Col 2xRow 16 Col 4
17Row 17 Col 1Row 17 Col 2xRow 17 Col 4
18Row 18 Col 1Row 18 Col 2123Row 18 Col 4
19Row 19 Col 1Row 19 Col 2123Row 19 Col 4
20Row 20 Col 1Row 20 Col 2123Row 20 Col 4
21
22
23
24
25Row 5 Col 1Row 5 Col 2123Row 5 Col 4
26Row 6 Col 1Row 6 Col 2123Row 6 Col 4
27Row 9 Col 1Row 9 Col 2123Row 9 Col 4
28Row 10 Col 1Row 10 Col 2123Row 10 Col 4
29Row 11 Col 1Row 11 Col 2123Row 11 Col 4
30Row 12 Col 1Row 12 Col 2123Row 12 Col 4
31Row 13 Col 1Row 13 Col 2123Row 13 Col 4
32Row 18 Col 1Row 18 Col 2123Row 18 Col 4
33Row 19 Col 1Row 19 Col 2123Row 19 Col 4
34Row 20 Col 1Row 20 Col 2123Row 20 Col 4
35
Sheet1
Cell Formulas
RangeFormula
A5:B20,D5:D20A5="Row "&ROW()&" Col " &COLUMN()
A25:D34A25=FILTER(A5:D20,C5:C20=H2,"")
Dynamic array formulas.


When you use the following double-click code (clicking just on column A of sheet 1)
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
        Cancel = True
        Application.EnableEvents = False
        Dim GoToCell As Range, a() As Variant, i As Long, c As Range
            ReDim a(1 To 4) As Variant
            i = 1
            For Each c In Range(Target.Address, Target.Offset(, 3))
               a(i) = c.Value
               i = i + 1
            Next c
            Set GoToCell = Sheet3.Range("A" & Sheet3.Cells(Rows.Count, "A").End(xlUp).Row + 1)
            GoToCell.Resize(1, 4).Value = a
        Application.EnableEvents = True
    End If
End Sub

You get the following result on sheet 3, when you double-click on column A of rows 1, 3 and 10 of the array returned by the Filter() function:
Book1
ABCD
1HDR1HDR2HDR3HDR4
2Row 5 Col 1Row 5 Col 2123Row 5 Col 4
3Row 9 Col 1Row 9 Col 2123Row 9 Col 4
4Row 20 Col 1Row 20 Col 2123Row 20 Col 4
5
Sheet3


Is this close to what you're looking for?
 
Upvote 1
See if this does what you want.
I have assumed that the FILTER formula you gave above is in cell A24, hence the code checking that the double-click is at least that far down the worksheet.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  With Target
    If .Column = 1 And .Row >= 24 Then
      Cancel = True
      .EntireRow.Copy
      Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlValues
      Application.CutCopyMode = False
    End If
  End With
End Sub
 
Upvote 1
Solution
@kevin9999 and @Peter_SSs Thank you for your answers but I haven't been able to test them because when I double click a cell in the "A" column it just goes into the cell. Nothing happens.
I'm pretty new to VBA so I'm not sure what I'm doing wrong. Yesterday, the code I included in my post worked (without doing the pasting part I need) and today none of the codes I have tried work.
Not sure what's different. For example in the image below I double clicked the "A" cell in the second result.

1685363677132.png
 
Upvote 0
We can't tell anything much from a picture. However, the first thing that I would try would be to close the workbook and also close right out of Excel and then start Excel again as your 'events' may have become disabled.
 
Upvote 0
@kevin9999 and @Peter_SSs,

Thank you so much for your help. Both your codes do what I need.
Now, as a first timer posting, I'm not sure what's the procedure in this case as far as "Mark as a solution" goes.
Can I mark both as a solution or do I need to pick one?

Thank you
 
Upvote 0
Can I mark both as a solution or do I need to pick one?
You can only mark one solution. In the case where 2 or more solutions achieve your requirements equally well, you should pick the most efficient, which is (usually) the routine with the fewer lines of code. In this case, that would be Peter's (which is the one I'd pick) (y) :)
 
Upvote 0
Thank you so much for your help. Both your codes do what I need.
You're welcome. Glad we could help. Thanks for the follow-up. :)


Can I mark both as a solution or do I need to pick one?
As Kevin has indicated, you can only mark one. If one seems more efficient to you then you likely used that one and would mark it. If efficiency seems equal then you might use the one that made most sense to you or the one that you felt you might be best able to alter if required later & then you might mark that one. In the end, it doesn't matter much as it is really just an indication to future readers that a solution was found in the thread and they can look through and see if any of the posts suit them.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top