You are here:

Excel/Macro to filter and copy data in new workbook

Advertisement


Question
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", _
 MultiSelect:=True)
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))

Next

End Sub

Thanks for your help!!

Answer
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:

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

Hope this helps,

Gulshan.
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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


GULSHAN PURSWANI

Expertise

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.

Experience

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

Education/Credentials
Bachelor of Commerce, Chartered Accountant from The Institute of Chartered Accountants of India

©2016 About.com. All rights reserved.