VBA - Export filtered data from selected file

Danoob

New Member
Joined
May 31, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,

I'm trying to export data and filtered them from a file selected by an user before copying and pasting it in one file. I want to :
- Filtered the data on columns F and H
- Then copy the data from B to L columns without the headers
I added an image of the type of data I want to export.
There are others things I want to do but I'm focusing on those two aspect at the moment
So far with what I found and understood on the internet I manage to do this :

I get an error while running it
Sub Test2()

Dim Fname As String
Dim SrcWbk As Workbook
Dim DestWbk As Workbook

Set DestWbk = ThisWorkbook

Fname = Application.GetOpenFilename(FileFilter:="Macro Files (*.xlsm*), *.xlsm*", Title:="Select a File")
If Fname = "False" Then Exit Sub
Set SrcWbk = Workbooks.Open(Fname)
With SrcWbk.Range("A1:AI1")
.autofilter Field:=6, Criteria1:="Bloqué"
.autofilter Field:=8, Criteria1:="JEB"
End With
SrcWbk.Sheets("Base").Range("B:AI").Copy DestWbk.Sheets("Sheet1").Range("A1")
SrcWbk.Close False

End Sub

I'm an newbie in VBA, I'm a bit lost right now and don't know where to go.
Thanks you for any help provide
 

Attachments

  • Capture d’écran 2022-05-31 112757.png
    Capture d’écran 2022-05-31 112757.png
    50 KB · Views: 11

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try and modify the line With SrcWbk.Range("A1:AI1") to
VBA Code:
With SrcWbk.Sheets("Base").Range("A1:AI1")
 
Upvote 0
Try and modify the line With SrcWbk.Range("A1:AI1") to
VBA Code:
With SrcWbk.Sheets("Base").Range("A1:AI1")
Thanks for your suggestion, I added it and now I don't have an error however it only takes the headers and don't seem to consider the autofilters I put whereas want it to copy the filtered data without the headers.
I appreciate your feedback
 
Upvote 0
Try
VBA Code:
Sub Test2()

Dim Fname As String
Dim SrcWbk As Workbook
Dim DestWbk As Workbook

Set DestWbk = ThisWorkbook

Fname = Application.GetOpenFilename(FileFilter:="Macro Files (*.xlsm*), *.xlsm*", Title:="Select a File")
If Fname = "False" Then Exit Sub
Set SrcWbk = Workbooks.Open(Fname)
lastL = SrcWbk.Sheets("Base").Cells(Rows.Count, "B").End(xlUp).Row
With SrcWbk.Range("A1:AI" & lastL)
    .AutoFilter Field:=6, Criteria1:="Bloqué"
    .AutoFilter Field:=8, Criteria1:="JEB"
End With
SrcWbk.Sheets("Base").Range("B2:AI" & lastL).Copy DestWbk.Sheets("Sheet1").Range("A1")
SrcWbk.Close False

End Sub
 
Upvote 0
Try
VBA Code:
Sub Test2()

Dim Fname As String
Dim SrcWbk As Workbook
Dim DestWbk As Workbook

Set DestWbk = ThisWorkbook

Fname = Application.GetOpenFilename(FileFilter:="Macro Files (*.xlsm*), *.xlsm*", Title:="Select a File")
If Fname = "False" Then Exit Sub
Set SrcWbk = Workbooks.Open(Fname)
lastL = SrcWbk.Sheets("Base").Cells(Rows.Count, "B").End(xlUp).Row
With SrcWbk.Range("A1:AI" & lastL)
    .AutoFilter Field:=6, Criteria1:="Bloqué"
    .AutoFilter Field:=8, Criteria1:="JEB"
End With
SrcWbk.Sheets("Base").Range("B2:AI" & lastL).Copy DestWbk.Sheets("Sheet1").Range("A1")
SrcWbk.Close False

End Sub
I think I finally found the solution thanks to your help @Anthony47
I added some little things that I realized I forgot and put some comments.
I put it there in case someone want to do something similar. Let me know if you have any suggestions :)

VBA Code:
Sub Test2()

Application.ScreenUpdating = False

'Variables definitions'
Dim Fname As String
Dim SrcWbk As Workbook
Dim DestWbk As Workbook
Set DestWbk = ThisWorkbook
Dim Lastrow As Long

Lastrow = DestWbk.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

'Allows to pick the desired XLSM file'
Fname = Application.GetOpenFilename(FileFilter:="Macro Files (*.xlsm*), *.xlsm*", Title:="Select a File")
If Fname = "False" Then Exit Sub

'Tell where to look in the selected file'
Set SrcWbk = Workbooks.Open(Fname)
lastL = SrcWbk.Sheets("Base").Cells(Rows.Count, "B").End(xlUp).Row

'Filter base on 2 criteria'
With SrcWbk.Sheets("Base").Range("A1:AI" & lastL)
    .AutoFilter Field:=6, Criteria1:="Bloqué"
    .AutoFilter Field:=8, Criteria1:="JEB"
End With

'Tell where to take the data to copy'
SrcWbk.Sheets("Base").Range("B2:AI" & lastL).Copy

'Copy the value from the file on the last row of the current sheet'
DestWbk.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.Goto Sheets("Sheet1").Range("A1")
Application.CutCopyMode = False

Application.ScreenUpdating = True

'Close the file where the data was extracted'
SrcWbk.Close False

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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