PROCV Excel - Use of connections

Asked

Viewed 604 times

1

Connections with other worksheets can be used in a PROCV ?

Ex:

I added a connection to another worksheet I use. There are columns. I can apply a PROCV or some other formula to consume data ?

inserir a descrição da imagem aqui

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

  • 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, 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.

1 answer

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:

inserir a descrição da imagem aqui

In your result sheet (result.xlsx), where you want your search to appear do the following steps:

  1. Insert a Query as below: inserir a descrição da imagem aqui

  2. Select the option Excel Files and click 'OK': inserir a descrição da imagem aqui

  3. Select your data file: inserir a descrição da imagem aqui

  4. 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: inserir a descrição da imagem aqui

  5. 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: inserir a descrição da imagem aqui

  6. Moving forward again you will be able to choose which sheet and in which cell to put the data: inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  1. "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: inserir a descrição da imagem aqui

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!

  • Evert, I can set the criteria to =$A2 ? I tried it here and it didn’t work

  • I updated my response. From there I suggest that you test the macro and place the variables dynamically so that the data is inserted in the location to be defined.

  • It worked out there @Kennedymota?

  • 1

    I’m using the connections now, but not in the ideal way I wanted. But it’s already solved. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.