简体   繁体   中英

How to use autofilter from one workbook to another?

I am trying to get a database from Workbook B to autofilter using some input from Workbook A. I am building the macro in Workbook A.

I have the following: Workbook A - Document were you start working Worrkbook B - Database, the final objective of this code is to import some info from Workbook B to Workbook A

I need the following:

  • By double clicking in a column from workbook A, workbook B should open (done)
  • The clicked value from workbook A will be saved as a variable, lets call it input_db (done)
  • Workbook B will autofilter based on input_db (not done, help required here!)
  • The required data is selected from workbook B and imported to workbook A, preferably with a double click as well (not done yet, but if you have any suggestion for this, Ill be gratefull ;))

The process should be repeated several times in a row.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Dim input_row, input_column As Integer
Dim input_db As String
Dim wbB, wbA As Workbooks

If Not (Application.Intersect(ActiveCell, [Links]) Is Nothing) Then 'Links is the name of the range where I need the code to be active
If ActiveCell.Value <> "" Then

input_row = ActiveCell.row
input_column = ActiveCell.Column
input_db = ActiveCell.Value

Set database = Workbooks.Open("Workbook B location")


ActiveWorkbook.ActiveSheet.Range("A9").AutoFilter Field:=1, Criteria:=input_db ' This bit doesnt work as expected


End If
End If

End Sub

As I can see you are using the wrong format of Autofilter. Try this line after amending as per your need. Autofilter takes in Criteria1 not Criteria.

ActiveWorkbook.ActiveSheet.Range("A9").AutoFilter Field:=1, Criteria1:=input_db

Also you can find more about Autofilter here: Link

Let us know if it still doesn't work.

Edit 2 :

You will have to open a input box and select the cell. You can follow the This Link for that.

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