VBA - filter based on specific value then copy relevant values in other columns

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Hi,

I have this dataset which has the following columns Location, Salary, Trees, Apple, Cost, Jack. I'm looking to be able to;
  1. Go to column "Salary".
  2. Filter for values = 2 in Salary Column
  3. Then extract only values from Location, Salary, Apple & Cost. NOT JACK and TREEs
Also, the column location changes sometimes so i'm using this (see below). is there an easier way to get the location of Location, Salary, Apple & Cost
Sub GoToColumn()

Set CWS = ActiveSheet
AColNum = Application.WorksheetFunction.Match("Salary", CWS.Rows(1), 0)
Cells(1, AColNum).Select

Example of dataset:

LocationSalaryTreesAppleCostJack
Washington
1​
0.78​
No
0.99​
0.87​
Washington
1​
0.60​
No
0.42​
0.51​
Washington
1​
0.17​
No
0.81​
0.86​
Washington
1​
0.68​
No
0.58​
0.64​
Washington
1​
0.34​
No
0.19​
0.31​
Washington
1​
0.72​
No
0.87​
0.48​
New York
2
0.59​
Yes
0.24
0.63​
New York
2
0.79​
Yes
0.87
0.96​
New York
2
0.08​
Yes
0.46
0.83​
New York
2
0.66​
Yes
0.20
0.30​
Washington
3​
0.45​
No
0.35​
0.12​
Washington
3​
0.34​
No
0.88​
0.87​
Washington
4​
0.02​
No
0.57​
0.41​
Washington
4​
0.37​
No
0.21​
0.23​
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi.
I would go with Find function to get column numbers.

VBA Code:
Sub FindColumns() 'Location, Salary, Apple & Cost
 Dim L As Long, S As Long, A As Long, C As Long
  L = Rows(1).Find("Location").Column
  S = Rows(1).Find("Salary").Column
  A = Rows(1).Find("Apple").Column
  C = Rows(1).Find("Cost").Column
End Sub
 
Upvote 0
Personally I would use Advanced Filter to do this, which lends itself very well to specifying which columns you want to copy. Try this on a copy of your data, and change the sheet names for the source and destination to suit. Assumes your data starts in column A and row 1 is a header row.

VBA Code:
Option Explicit
Sub Copy_Some_Columns()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ change to actual source sheet name
    Set ws2 = Worksheets("Sheet2")  '<~~ change to actual destination sheet name
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
    
    'Get the copy to destination
    ws2.UsedRange.ClearContents
    Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
    rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")
    
    'Filter and copy
    Dim SalCol As Long
    SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
    Set rngList = ws1.Range("A1").CurrentRegion
    With rngList
        Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
        rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
        .AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    End With
    
    'Tidy up
    ws2.Cells.Columns.AutoFit
    rngCriteria.ClearContents
End Sub
 
Upvote 0
Personally I would use Advanced Filter to do this, which lends itself very well to specifying which columns you want to copy. Try this on a copy of your data, and change the sheet names for the source and destination to suit. Assumes your data starts in column A and row 1 is a header row.

VBA Code:
Option Explicit
Sub Copy_Some_Columns()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ change to actual source sheet name
    Set ws2 = Worksheets("Sheet2")  '<~~ change to actual destination sheet name
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
  
    'Get the copy to destination
    ws2.UsedRange.ClearContents
    Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
    rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")
  
    'Filter and copy
    Dim SalCol As Long
    SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
    Set rngList = ws1.Range("A1").CurrentRegion
    With rngList
        Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
        rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
        .AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    End With
  
    'Tidy up
    ws2.Cells.Columns.AutoFit
    rngCriteria.ClearContents
End Sub
This is amazing!!! Just one thing, Instead of moving it to a new sheet, how do i keep it in the file that contains the code? one file that contains the code and another that contains the data
 
Upvote 0
This is amazing!!! Just one thing, Instead of moving it to a new sheet, how do i keep it in the file that contains the code? one file that contains the code and another that contains the data
Personally I would use Advanced Filter to do this, which lends itself very well to specifying which columns you want to copy. Try this on a copy of your data, and change the sheet names for the source and destination to suit. Assumes your data starts in column A and row 1 is a header row.

VBA Code:
Option Explicit
Sub Copy_Some_Columns()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ change to actual source sheet name
    Set ws2 = Worksheets("Sheet2")  '<~~ change to actual destination sheet name
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
   
    'Get the copy to destination
    ws2.UsedRange.ClearContents
    Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
    rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")
   
    'Filter and copy
    Dim SalCol As Long
    SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
    Set rngList = ws1.Range("A1").CurrentRegion
    With rngList
        Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
        rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
        .AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    End With
   
    'Tidy up
    ws2.Cells.Columns.AutoFit
    rngCriteria.ClearContents
End Sub

This is the code I'm working with. Using open_file_dialog to get the source data but also getting workbook and worksheet name that contains the macro code. Unfortunately im erroring out at "ws2.UsedRange.ClearContents" run time error "91" Object variable or with block variable not set. Any idea what the issue is?

Option Explicit
Global ws2 As Worksheet
Global ws1 As Worksheet
Public CFile As String
Public strFile As String
Public lastRow As Long
Sub open_file_dialog()
Dim strFile As String, CFile As String, ws1 As Worksheet, ws2 As Worksheet

CFile = Application.ActiveWorkbook.FullName
ActiveSheet.Select
Set ws1 = ThisWorkbook.Worksheets(1)

strFile = Application.GetOpenFilename()
Workbooks.Open (strFile)
ActiveSheet.Select
Set ws2 = ActiveSheet

End Sub
Sub GoToColumnAndSelectValues()
Dim ws1 As Worksheet, ws2 As Worksheet
'Set ws1 = Worksheets("Sheet1")
'Sheets.Add.Name = "Sheet2"
'Set ws2 = Worksheets("Sheet2")
Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range

'Get the copy to destination
ws2.UsedRange.ClearContents
Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")

'Filter and copy
Dim SalCol As Long
SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
Set rngList = ws1.Range("A1").CurrentRegion
With rngList
Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
.AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
End With

'Tidy up
ws2.Cells.Columns.AutoFit
rngCriteria.ClearContents

End Sub
 
Upvote 0
This is amazing!!! Just one thing, Instead of moving it to a new sheet, how do i keep it in the file that contains the code? one file that contains the code and another that contains the data
Now you're confusing me :unsure: You'll need to tell me the name of the sheet (in the file that contains the code?) and the cell location where you want the imported data to go to.
 
Upvote 0
Try this.
VBA Code:
Option Explicit
Sub Copy_Some_Columns_V2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim strFile As String
    strFile = Application.GetOpenFilename()
    Workbooks.Open (strFile)
    Set ws1 = ActiveWorkbook.Sheets(1)
    
    Set ws2 = ThisWorkbook.Worksheets("Sheet1")  '<~~ change to actual destination sheet name
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
    
    'Get the copy to destination
    ws2.UsedRange.ClearContents
    Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
    rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")
    
    'Filter and copy
    Dim SalCol As Long
    SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
    Set rngList = ws1.Range("A1").CurrentRegion
    With rngList
        Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
        rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
        .AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    End With
    
    'Tidy up
    rngCriteria.ClearContents
End Sub
 
Upvote 1
Solution
Now you're confusing me :unsure: You'll need to tell me the name of the sheet (in the file that contains the code?) and the cell location where you want the imported data to go to.
Sorry the file name that contains the code is "XmlTesting.xlsm" and sheet name is "Task Details". This is where i want the imported data to go. Cell location is A2 (wont be needing headers)
 
Upvote 0
Try this.
VBA Code:
Option Explicit
Sub Copy_Some_Columns_V2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim strFile As String
    strFile = Application.GetOpenFilename()
    Workbooks.Open (strFile)
    Set ws1 = ActiveWorkbook.Sheets(1)
   
    Set ws2 = ThisWorkbook.Worksheets("Sheet1")  '<~~ change to actual destination sheet name
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
   
    'Get the copy to destination
    ws2.UsedRange.ClearContents
    Set rngCopyTo = ws2.Range("A1").Resize(1, 4)
    rngCopyTo.Value2 = Array("Location", "Salary", "Apple", "Cost")
   
    'Filter and copy
    Dim SalCol As Long
    SalCol = WorksheetFunction.Match("Salary", ws1.Rows(1), 0)
    Set rngList = ws1.Range("A1").CurrentRegion
    With rngList
        Set rngCriteria = rngList.Offset(, .Columns.Count).Resize(2, 1)
        rngCriteria.Cells(2).FormulaR1C1 = "=RC" & SalCol & "=2"
        .AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo
    End With
   
    'Tidy up
    rngCriteria.ClearContents
End Sub
This is perfect, thank you!!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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