1
Since you’re using connections, I suggest you use the Microsoft Query, as follows:
I have a spreadsheet (.xlsx data) with your data, example:
In your result sheet (result.xlsx), where you want your search to appear do the following steps:
If the name of the tab/sheet does not appear, click on Options and check the option "System Tables". Select the spreadsheet and click the right arrow as sequence in the image below:
After clicking on "Next", select the column that should be filtered and select the text/number for the filter, clicking on "Next" will have the possibility to sort your result by certain column:
Moving forward again you will be able to choose which sheet and in which cell to put the data:
- "OK" and your data will be in your result sheet. When entering a new data into your Data sheet, go to your Results sheet and click Update to search for new data:
To automate this process, you can try to "write" a macro and then edit to meet your demand by creating new automated connections.
EDITION 1
To enter a criterion could use the following function (Sub) down below:
Sub busca_dados_externos()
Dim CRITERIO As String
CRITERIO = Range("A2").Text
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=C:\Users\m362168\OneDrive\Projetos\Budhi\Excel\Query\dados.xlsx;DefaultDir=C:\Users\m362168\OneDrive\Projet" _
), Array("os\Budhi\Excel\Query;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;") _
), Destination:=Range("$B$2")).QueryTable
.CommandText = Array( _
"SELECT `Plan1$`.teste, `Plan1$`.descricao, `Plan1$`.valor" & Chr(13) & "" & Chr(10) & "FROM `C:\Users\m362168\OneDrive\Projetos\Budhi\Excel\Query\dados.xlsx`.`Plan1$` `Plan1$`" & Chr(13) & "" & Chr(10) & "WHERE (`Plan1$`.teste='" & CRITERIO & "')" & Chr(13) & "" & Chr(10) & "ORDER BY `Plan1$`.d" _
, "escricao, `Plan1$`.valor")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Tabela_Consulta_de_Excel_Files_1"
.Refresh BackgroundQuery:=False
End With
Range("C10").Select
End Sub
Of course you can place all variables (FILE, PATH, COLUMNS, CRITERIA, DESTINATION etc) dynamically, to have a more complete function.
I hope I’ve helped!
Do you need to use the connections or could you just use a relative path? Because Procv can be used to search for other sheets/files in the same way as it is used in the same spreadsheet.
– Evert
If you choose to use a connection to another file you can use query directly in the file searching for the data you want. If you put in the question the types of data and an example of the data you need to extract I think you will have a more precise help.
– Evert
@Evert, good morning. I want to use connections. Can you help me with the Querys question? I will have 1 number in plan A that I will use as the search key in plan B. And I want to return the value of a column. Same as PROCV but through connections.
– Kennedy Mota