Excel에서 VBA를 사용하여 Vlookup을 만들려고합니다.VBA - Vlookup - 여러 열 반환
난에 '2 열'에 대한 'Sheet1의'에 '컬럼 1'을 찾아 할 '시트 2'
나는 또한 시트에 여러 열을 반환 할1 - 3,4,5,6 (시트 2에서)
당신이 나를 도와 드릴까요?
Excel에서 VBA를 사용하여 Vlookup을 만들려고합니다.VBA - Vlookup - 여러 열 반환
난에 '2 열'에 대한 'Sheet1의'에 '컬럼 1'을 찾아 할 '시트 2'
나는 또한 시트에 여러 열을 반환 할1 - 3,4,5,6 (시트 2에서)
당신이 나를 도와 드릴까요?
내가 대신 VLOOKUP의 쉽고 SQL 쿼리 테이블을 사용하는 것이 훨씬 빠른 것 같아요. 당신이 원하는 쿼리 테이블을 만드는 1) 먼저 호출 두 번째 매크로 : 나는 두 개의 매크로 코드를 제시 아래
. 2) second는 표시된 ado SQL 쿼리 문 (sql_stmt 문자열에 표시됨)을 실행하고 지정된 시트 및 범위에 붙여 넣는 하위 프로 시저입니다.
은 SQL_stmt를 문자열 definiton에서는 적절한 열 헤더에 "sheetX_columnXheader"를 변경해야합니다. 당신이 다른 시트에 결과를 얻고 싶은 경우에은 서로 다른 두 번째 매개 변수 sql_query 하위 프로 시저를 호출해야합니다. 다른 열을 결과로 가져 오거나 다른 열의 데이터와 일치 시키려면 sql_stmt 문자열을 적절한 ado SQL 쿼리 문으로 변경해야합니다. (- ...> 참조 도구)
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
활성화하는 것을 잊지 마십시오
VBA 편집기에서 이상 "마이크로 소프트 액티브 데이터는 2.8 개체 라이브러리". 각 시트의 데이터 최대 크기는 256 열과 65535 행입니다. 엑셀 2007희망과 작품이 도움이됩니다.
무엇을 시도 했습니까 (코드 게시)? 왜 작동하지 않았습니까 (오류 메시지, 예상 된 결과를 반환하지 않는지 등)? 또한 가능한 경우 일부 샘플 데이터를 제공 할 수 있습니까? – tigeravatar
안녕하세요. 회신 해 주셔서 감사합니다. 내 문제가있는 스프레드 시트를 첨부했습니다. 불행히도 나는 VB에서 처음이므로 너무 많이 시도 할 수 없었다! 스프레드 시트의 Sheet1에서 더 자세히 설명했습니다. 당신의 도움을 주셔서 감사합니다!! – user2682287
죄송합니다. 파일을 어떻게 첨부합니까? – user2682287