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

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,614
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I am using below code by @Anthony47, which is working fine, now I just want to modify code and instead of copying data in to clipboard I want to save each filtered name as separate image in a folder for that it should ask for destination folder and want to name each image same as the name filtered

and I don't want a separate message after each filtered data is saved, I just want final message when all the names from column J filtered and saved.

VBA Code:
Sub SeqToClip_Pict()
Dim fRange As Range, fRC As Long
Dim I As Long, cFilt
'
Set fRange = Range(Range("E1"), Range("E1").End(xlDown).Offset(1, 0))   'Assuming the table starts in E1
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, -1), 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



My Data looks like this
Book1
CDEFGHIJK
1DateAmountName
29/28/20225A
39/28/20225A
4110B
59/29/202210B
69/30/202215C
79/30/202215C
810-01-202220D
910-01-202220D
10125E
1110-02-202225E
12
13150Total Quantity
144500Total Amount
15
16A
17B
18
19
20
21
Sheet1
Cell Formulas
RangeFormula
C4,C10C4=B3+1
D13D13=SUBTOTAL(9,D2:D11)
D14D14=D13*30



Reference

 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Cross posted at

 
Upvote 0
The solution has been also published on excelforum.

My proposition (both worksheets in the same workbook, and pictures in the same folder where the workbook is stored. If you do not need worksheets for reference delete 3 lines as noted in code and substitute new name assignment with sheet deleting:
PS. In naming a picture with the content, I added time and date
Code:
.Export Filename:=ThisWorkbook.Path & "\" & cFilt & Format(Now, "_yymmdd_hhmmss") & ".png", Filtername:="png"
One can save just under stright name, but then preexisting file with such name shall be deleted from the folder before
Code:
.Export Filename:=ThisWorkbook.Path & "\" & cFilt & ".png", Filtername:="png"

Final comment - note that in your file some dates are real Excel dates, some are just texts, which look like a date. Try changing all dates format to some other like dd mmmm yyyy and see that only excel dates wil follow that change, while texy s will remain unchanged

And the whole code:
Code:
Sub SeqToPNGPictures()
Dim fRange As Range, fRC As Long
Dim I As Long, cFilt As String, wks As Worksheet, table As Range
Dim pic As Picture, cht As ChartObject, lWidth As Long, lHeight As Long

Set wks = Sheets("Sheet1")
wks.Activate: wks.AutoFilterMode = False
Set fRange = Range(Range("E1"), Range("C1").End(xlDown).Offset(0, 2))   'Assuming the table starts in E1
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
      On Error Resume Next 'these 3 lines down to on error got can be deleted if no sheet saving is planned
        Application.DisplayAlerts = False: Sheets(cFilt).Delete: Application.DisplayAlerts = True
      On Error GoTo 0
      Range(fRange.Cells(1, -1), fRange.Cells(fRC + 3, fRange.Columns.Count)).Copy
      With ActiveWorkbook.Sheets.Add
        .Range("C1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        .Columns("C:E").AutoFit
        Set table = .Range(.Cells(1, "C"), .Cells(.Rows.Count, "E").End(xlUp))
        table.CopyPicture xlScreen, xlPicture
        lWidth = table.Width
        lHeight = table.Height
        Set cht = .ChartObjects.Add(Left:=0, Top:=0, Width:=lWidth, Height:=lHeight)
        cht.Activate
        With cht.Chart
          .Paste
          .Export Filename:=ThisWorkbook.Path & "\" & cFilt & Format(Now, "_yymmdd_hhmmss") & ".png", Filtername:="png"
        End With
        cht.Delete
        .Name = cFilt
        'or if you dont need sheets, instead of .Name = cFilt  just:
        'Application.DisplayAlerts = False : .Delete : Application.DisplayAlerts = True
      End With
      wks.Activate
    End If
Next I
fRange.AutoFilter Field:=1
Application.CutCopyMode = False
MsgBox ("Completed")
End Sub
 

Attachments

  • A_240904_140018.png
    A_240904_140018.png
    2.5 KB · Views: 1
  • B_240904_140018.png
    B_240904_140018.png
    2.5 KB · Views: 1
  • C_240904_140018.png
    C_240904_140018.png
    2.6 KB · Views: 1
Upvote 0
Solution
Thank you very much Kaper, working perfect! (y)
spent around 3-4 hours on ChatGPT to get this done, but no got it working!
Thank you again for your time to write this amazing code! 👨‍💻
 
Upvote 0
Hi Kaper,

Sorry but when I tried above code in a new module in a new worksheet, it is not working,

I am getting below error

1725458610282.png


in this line

VBA Code:
table.CopyPicture xlScreen, xlPicture


but when I use the spreadsheet which you shared in ExcelForum it is working perfect!
 
Upvote 0
probably there are some differences in formatting or something like that. Make sure your data posted with xl2bb is really representative.
 
Upvote 0
I just copied the same spreadsheet which you shared and now it's working.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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