简体   繁体   中英

Filter the data based on another workbook cell value using VBA

As i'm still learning, i got stuck in applying the condition to filter two columns in target sheet based on the source workbook cell value.

Eg. source: column A1 having Q2 and column b2 having d104 i need to use this as filter in target workbook and delete the existing visible filtered values and replace it with copied data.

I have the logic to just replace the data without filters. but how to achieve using the filters?



Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim CopyLastRow As Integer
Dim DestlastRow As Integer

    '1. open the workbook to copy from
    Workbooks.Open "C:\Forecast.xlsx" -- target
     '2. Define each workbook
    Set wsCopy = ThisWorkbook.Sheets("Input Data") --source
    Set wsDest = Workbooks("Imports PY Plan Forecast.xlsx").Sheets("Source")
    
    
    '3. Define last row in source data
    CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row

    ThisWorkbook.Worksheets("Input Data").Range("A2:F" & CopyLastRow).Copy wsDest.Range("A2")
    
    '4. close and save source file
    Workbooks("Imports PY Plan Forecast.xlsx").Close SaveChanges:=True
    


End Sub```

In order to achiever filter row, and delete and paste value, you can write code like following after set workbook is done:

'Filter column A and column B based on value
Worksheets("Input Data").Range("A1:AE" & CopyLastRow).AutoFilter Field:=1, Criteria1:="Q2"
Worksheets("Input Data").Range("A1:AE" & CopyLastRow).AutoFilter Field:=2, Criteria1:="d104"

'Assuming your data start from row 2, will only delete visible cell
Worksheets("Input Data").range("A2:A" & CopyLastRow).entirerow.delete

'And copy value in visible cell using special cell function
ThisWorkbook.Worksheets("Input Data").Range("A2:F" & CopyLastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A2")


Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim wsfilter As Worksheet
Dim CopyLastRow As Integer
Dim DestlastRow As Integer
Dim ws As Worksheet, rng As Range, LstRw As Long

    '1. open the workbook to copy from
    Workbooks.Open "C:Forecast.xlsx"
     '2. Define each workbook
    Set wsCopy = ThisWorkbook.Sheets("Input Data")
    Set wsDest = Workbooks("Forecast.xlsx").Sheets("Source")
    Set wsfilter = ThisWorkbook.Sheets("HR - Source")
    '3. Define last row in source data
    CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
    
    '4.Filter column A and column B based on value
    wsDest.Range("A1:F" & CopyLastRow).AutoFilter Field:=3, Criteria1:=wsfilter.Range("E1").Value
    wsDest.Range("A1:F" & CopyLastRow).AutoFilter Field:=5, Criteria1:=wsfilter.Range("D2").Value & "*"
    
    '5.Delet Visible data
    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    Application.DisplayAlerts = True
    
    '6. Copy data from Input Data to Forecast Source
    ThisWorkbook.Worksheets("Input Data").Range("A2:F" & CopyLastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A2")
    On Error Resume Next
        wsDest.ShowAllData
    On Error GoTo 0

     '7. close and save source file
        Workbooks("Forecast.xlsx").Close SaveChanges:=True
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM