You are here:

Excel/Macro to filter and copy data in new workbook


Hi Gulshan,

Hope you are doing well!!

Below is my code to open an workbook from the path specified in Range B1. It is working fine.
Need: I would like to have a code to filter the range from A:M in the opened workbook with criteria "JPMVEC" copy and paste the visible cells in new workbook thereby saving it in the spath1 mentioned in the master sheet.

Sub Segregate()
'Consider this macro ofor
Dim sh As Worksheet, v As Variant, i As Long
Dim r As Range, bk As Workbook, sh1 As Worksheet
Dim sPath As String, vv As Variant
Dim sPath1 As String
Dim rr As Range, r1 As Range
Dim r2 As Range

'mystring = E3
Dim myselection As Range

sPath = Sheets("Sheet1").Range("B1").Value
sPath1 = Sheets("Sheet1").Range("B2").Value
ChDrive sPath
ChDir sPath
vv = Application.GetOpenFilename( _
 filefilter:="Excel Files (*.xl*),*.xl", _
 Title:="Select one or more files to process", _
If Not IsArray(vv) Then
 MsgBox "You chose to cancel"
 Exit Sub
End If
For i = LBound(vv) To UBound(vv)
Set bk = Nothing
On Error Resume Next
Application.DisplayAlerts = False

Set bk = Workbooks.Open(Filename:=vv(i))


End Sub

Thanks for your help!!

Hi Divakar,

You can use 2 more pieces of code to make this happen:
1. select the workbook you want to open: workbooks.("xxxxx").activate
2. From that workbook, filter and copy the data by using the below code:

   ActiveSheet.Range("$A$1:$A$8").AutoFilter Field:=1, Criteria1:="JPMVEC"
   Range(Selection, Selection.End(xlDown)).Select
3. Select the original workbook where you started and paste the data there.

Hope this helps,

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts




I can answer questions related to the following topics: 1. MS Excel - Creating and Linking Formulae, Running Pivot Tables, Vlookup etc. 2. Macros / VBA - Creating Macros to do specific jobs. Importing / Exporting / Validating Data in excel through Macros.


I've been working on excel for over 10 years and on VBA macros for over 3 years now.

Bachelor of Commerce, Chartered Accountant from The Institute of Chartered Accountants of India

©2016 All rights reserved.