VBA Filter Names from List One by One and then Copy filtered data

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,538
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I have a data like below

I a VBA to Filter Names from List (J16:J20) One by One and then Copy the filtered data:

Book1
CDEFGHIJKL
1NameAmount
2A5
3A5
4B10
5B10
6C15
7C15
8D20
9D20
10E25
11E25
12
13Total Quantity150
14Total Amount4500
15
16A
17B
18C
19D
20E
21
22
23
24
25
Sheet1
Cell Formulas
RangeFormula
E13E13=SUBTOTAL(9,E2:E11)
E14E14=E13*30
 
Hii

Getting below error

1664349030407.png
I forgot to mention that the new code still rely on Function SetClipBoardText


One another thing can you please update, current filter is based on exact match, it is case sensitive, can we make it case unsensitive??
and for example in excel inbuilt filter when we search in filter "John", then we will get all the results for "John" like, "John wick Other text", "Other Text John Wick" etc.
Can we modify the code so that it can search everything based on value in column J like excel inbuilt filter?
The following penultimate version filters using the "Contain" method of Excel filter and is case insensitive:
VBA Code:
Sub SeqToClip_V3()
Dim fRange As Range, kArea As Range
Dim fArr As String, I As Long, cFilt, J As Long
'
Set fRange = Range(Range("D1"), Range("D1").End(xlDown).Offset(0, 1))   'Assuming the table starts in D1
'
For I = 16 To 1000
    cFilt = Cells(I, "J").Value
    If Len(cFilt) = 0 Then Exit For
    fArr = ""
    For J = 1 To fRange.Rows.Count
''        If fRange.Cells(J, 1).Value = cFilt Then
        If InStr(1, fRange.Cells(J, 1).Value, cFilt, vbTextCompare) > 0 Then
            fArr = fArr & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf
        End If
    Next J
    DoEvents
'
    If Len(fArr) > 2 Then
        fArr = fArr & fRange.Cells(J + 1, 1).Value & " - " & fRange.Cells(J + 1, 2).Value & vbCrLf
        fArr = fArr & fRange.Cells(J + 2, 1).Value & " - " & fRange.Cells(J + 2, 2).Value & vbCrLf
        SetClipBoardText (fArr)   'fRange.Cells(2, 1).Resize(fRange.Rows.Count - 2, 2).SpecialCells(xlCellTypeVisible).Value)
        Beep
        MsgBox ("Copy the clipboard data into WhatsApp Web; destination: " & Cells(I, "J").Value)
    End If
'
Next I
MsgBox ("Completed")
End Sub


Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function
It already include the code for Function SetClipBoardText

Of course, with that approach, when you filter for John will also get Johnson (just to mention one of the many possible situations)
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Anthony,
It works, but does not consider subtotal, it gives total value from the bottom of entire data and not a subtotal for filtered data:

Name - Amount
A - 5
A - 5
Total Quantity - 150
Total Amount - 4500
 
Upvote 0
:(Grrrrr...

This code seem to do the job:
VBA Code:
Sub SeqToClip_V4()
Dim fRange As Range, kArea As Range
Dim fArr As String, I As Long, cFilt, J As Long
'
Set fRange = Range(Range("D1"), Range("D1").End(xlDown).Offset(0, 1))   'Assuming the table starts in D1
'
For I = 16 To 1000
    cFilt = Cells(I, "J").Value
    If Len(cFilt) = 0 Then Exit For
    fRange.AutoFilter Field:=1, Criteria1:="*" & cFilt & "*"
    fArr = ""
    For J = 1 To fRange.Rows.Count
''        If fRange.Cells(J, 1).Value = cFilt Then
        If InStr(1, fRange.Cells(J, 1).Value, cFilt, vbTextCompare) > 0 Then
            fArr = fArr & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf
        End If
    Next J
    DoEvents
'
    If Len(fArr) > 2 Then
        fArr = fArr & fRange.Cells(J + 1, 1).Value & " - " & fRange.Cells(J + 1, 2).Value & vbCrLf
        fArr = fArr & fRange.Cells(J + 2, 1).Value & " - " & fRange.Cells(J + 2, 2).Value & vbCrLf
        SetClipBoardText (fArr)   'fRange.Cells(2, 1).Resize(fRange.Rows.Count - 2, 2).SpecialCells(xlCellTypeVisible).Value)
        Beep
        MsgBox ("Copy the clipboard data into WhatsApp Web; destination: " & Cells(I, "J").Value)
    End If
'
Next I
    fRange.AutoFilter Field:=1

MsgBox ("Completed")
End Sub


Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function
 
Upvote 0
Hi Anthony,

This is working perfectly.

And this is the final....

Can you please add one more thing if the data is like below how to include column C as well or can you please tell me which line to modify to include more columns to copy:

Book3
BCDEF
1DateNameAmount
228-09-2022A5
328-09-2022A5
429-09-2022B10
529-09-2022B10
630-09-2022C15
730-09-2022C15
801-10-2022D20
901-10-2022D20
1002-10-2022E25
1102-10-2022E25
12
13Total Quantity150
14Total Amount4500
15
16
Sheet1
Cell Formulas
RangeFormula
C4,C10C4=C3+1
E13E13=SUBTOTAL(9,E2:E11)
E14E14=E13*30
 
Upvote 0
If you wish to add columns to the table then you have to work on the line fArr = fArr & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf

In the given code, fRange.Cells(J, 1) refers to column D and fRange.Cells(J, 2) to column E; to refer to column C you may well use fRange.Cells(J, 0); thus the line could become
Code:
fArr = fArr & fRange.Cells(J, 0).Value & " - " & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf

However, if column C is a date you need to format its content or you will see a number; thus:
Code:
fArr = fArr & Format(fRange.Cells(J, 0).Value, "dd-mmm-yyyy") & " - " & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf
 
Upvote 0
Hi Anthony

Thank you very much for your time and great help, this is finally what I wanted.

Your support was great in modifying code again and again.

The final code which is working:

VBA Code:
Sub SeqToClip_V4()
Dim fRange As Range, kArea As Range
Dim fArr As String, I As Long, cFilt, J As Long
'
Set fRange = Range(Range("D1"), Range("D1").End(xlDown).Offset(0, 1))   'Assuming the table starts in D1
'
For I = 16 To 1000
    cFilt = Cells(I, "J").Value
    If Len(cFilt) = 0 Then Exit For
    fRange.AutoFilter Field:=1, Criteria1:="*" & cFilt & "*"
    fArr = ""
    For J = 1 To fRange.Rows.Count
''        If fRange.Cells(J, 1).Value = cFilt Then
        If InStr(1, fRange.Cells(J, 1).Value, cFilt, vbTextCompare) > 0 Then
            fArr = fArr & fRange.Cells(J, 0).Value & " - " & fRange.Cells(J, 1).Value & " - " & fRange.Cells(J, 2).Value & vbCrLf
        End If
    Next J
    DoEvents
'
    If Len(fArr) > 2 Then
        fArr = fArr & fRange.Cells(J + 1, 1).Value & " - " & fRange.Cells(J + 1, 2).Value & vbCrLf
        fArr = fArr & fRange.Cells(J + 2, 1).Value & " - " & fRange.Cells(J + 2, 2).Value & vbCrLf
        SetClipBoardText (fArr)   'fRange.Cells(2, 1).Resize(fRange.Rows.Count - 2, 2).SpecialCells(xlCellTypeVisible).Value)
        Beep
        MsgBox ("Copy the clipboard data into WhatsApp Web; destination: " & Cells(I, "J").Value)
    End If
'
Next I
    fRange.AutoFilter Field:=1

MsgBox ("Completed")
End Sub


Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function
 
Upvote 0
I have one more question:

What is the difference between the general copy with Ctrl + C and the copying with the code above?

Because when I manually copy the data with Ctrl + C

and paste it to WhatsApp chat, it pastes as image like below

1664450268806.png



And when I run the code and paste it, it past as values as below:

1664450348891.png
 
Upvote 0
A) The macro read the value from the cell and put it into a string (fArr); then the string is pushed into the windows clipboard (Function SetClipBoardText); that clipboard is then available to external applications (the WhatsApp page, in your case).
B) Contr-c copy the range into the office clipboard; you have several options when pasting the information within Excel or other Office applications, but when you operate through external graphic applications you get an image of the range with its content
In shots: A and B do different things :)
 
Upvote 0
Ok thanks for the explanation.

Can we make our code to work for option B ;)
I mean just like Ctrl + C
 
Upvote 0
Copying as image would be even simpler; for example:
Code:
Sub SeqToClip_Pict()
Dim fRange As Range, fRC As Long
Dim I As Long, cFilt
'
Set fRange = Range(Range("D1"), Range("D1").End(xlDown).Offset(0, 1))   'Assuming the table starts in D1
fRC = fRange.Rows.Count
'
For I = 16 To 1000
    cFilt = Cells(I, "J").Value
    If Len(cFilt) = 0 Then Exit For
    fRange.AutoFilter Field:=1, Criteria1:="*" & cFilt & "*"
    If fRange.SpecialCells(xlCellTypeVisible).Count > 2 Then             'See message 1
        Range(fRange.Cells(1, 0), fRange.Cells(fRC + 3, fRange.Columns.Count)).Copy     'See message 2
        Beep
        MsgBox ("Copy the clipboard data into WhatsApp Web; destination: " & Cells(I, "J").Value)
    End If
Next I
fRange.AutoFilter Field:=1
Application.CutCopyMode = False
MsgBox ("Completed")
End Sub
Notes:
1) The macro (as well the ones I suggested before) will not output a message if there are no filtered rows; if this is a problem then you need to remove the If fRange.SpecialCells(xlCellTypeVisible).Count > 2 Then /End If lines (2 lines)
2) The code, as uses fRange.Cells(1, 0) for the starting cell, already includes column C in the copy

Try...
 
Upvote 0
Solution

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

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