w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
VBA - Vlookup - Return Multiple Columns

I think it would be easier and much faster to use sql query table, instead of vlookup.

Below I present code with two macros: 1) First call second macro that makes query table you want. 2) Second is a subprocerude that executes indicated ado sql query statement (indicated in sql_stmt string) and pastes it to indicated sheet and range.

In sql_stmt string definiton you must change "sheetX_columnXheader" to adequate columns headers.

If you want to get results in different sheet you need to call sql_query subprocedure with different second parameter. If you want to get other columns as a result or match data on different columns you must change sql_stmt string to adequate ado sql query statement.

Option Explicit
Sub matching_data()

Dim sqlstmt As String

On Error GoTo error

Application.ScreenUpdating = False

sqlstmt = "SELECT a.[sheet1_column1header], b.[sheet2_column2header],
b.[sheet2_column3header], b.[sheet4_column2header] FROM [sheet1$] a LEFT
JOIN [sheet2$] b ON a.[sheet1_column1header]=b.[sheet2_column1header]"
sql_query sqlstmt, "new_sheet", "A1"

'ending
Application.ScreenUpdating = True
MsgBox ("Finished")
Exit Sub

'error message
error:
MsgBox ("Unknown error")
Application.ScreenUpdating = True
End Sub


'subprocedure that executes ado sql query statement and pastes results in
indicated range and sheet
Public Sub sql_query(ByVal sqlstmt As String, ByVal sheet_name As String,
ByVal target1 As String)

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim connstring As String
Dim qt As QueryTable
Dim tw_path As String
Dim is_name As Boolean
Dim sh As Worksheet

On Error GoTo error
'''adding sheet if there is no sheet with indicated name
is_name = False
For Each sh In ThisWorkbook.Worksheets
    If sh.Name = sheet_name Then is_name = True
Next
If is_name = False Then
ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)).Name
= sheet_name

''' connection
tw_path = ThisWorkbook.path & "" & ThisWorkbook.Name
connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tw_path
& ";Extended Properties=Excel 8.0;Persist Security Info=False"

''' making database
Set conn = New ADODB.Connection
conn.ConnectionString = connstring
conn.Open

'''executing statement
Set rs = New ADODB.Recordset
rs.Source = sqlstmt
rs.ActiveConnection = conn
rs.Open

'''saving results
ThisWorkbook.Worksheets(sheet_name).Activate
Set qt = Worksheets(sheet_name).QueryTables.Add(Connection:=rs,
Destination:=Range(target1))
qt.Refresh

'''ending
ending:
If rs.State <> adStateClosed Then rs.Close
conn.Close
If Not rs Is Nothing Then Set rs = Nothing
If Not conn Is Nothing Then Set conn = Nothing
Set qt = Nothing

Exit Sub

'
error:
MsgBox ("Unknown error occured in sql query subprocedure")
GoTo ending
End Sub

Remember to activate "Microsoft ActiveX data object 2.8 library" or higher in VBA editor (tools -> references...). Keep in mind, that maximum size for data in each sheet is 256 columns and 65535 rows. Works with Excel 2007.

Hope, this will help.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.