나는 이런 것들에 관해서 조금 초보자입니다. 그래서 이것이 어리석은 질문 인 경우 사과드립니다.Excel에서 VBA 변수 함수를 사용하여 SQL 쿼리 호출
VBA에서 SQL 쿼리를 실행해야합니다. 쿼리는 함수에 VBA 변수가 포함되어 있기 때문에 약간 이상합니다. 그렇지 않으면 모든 것이 아주 간단합니다. VBA는 쿼리를 호출 한 다음이를 클라이언트 Excel 문서에 삽입해야합니다.
Access 내에서 쿼리를 실행할 때마다 정상적으로 함수가 올바른 값을 반환하고 열을 필터링합니다. Excel에서 VBA에서 실행할 때마다 "Run-Time Error "3085"
라고 표시됩니다. 식의 정의되지 않은 함수 'CutOff'.
나는 정보를 찾고 Access 2003에 이런 종류의 일을하는 데 종종 문제가 있다고 이전 사이트에서 발견했지만 2010 년을 실행하고 있습니다. 문제가 해결 될 수 있기를 바라며 조언을 주시면 감사하겠습니다. 다음과 같이
쿼리는 다음과 같습니다
SELECT [<TableName>].ID...*
FROM [<TableName>]
WHERE ((([<TableName>].ID)>CutOff()))
ORDER BY [<TableName>]].ID;
Public Function Cutoff()
Dim WB1 As Excel.Workbook, WS1 As Excel.Worksheet
Dim y As Long
Set WB1 = Workbooks.Open("C:\filepath.z.xlsm")
Set WS1 = WB1.Sheets("Sheet2")
y = WS1.Range("A1").End(xlDown).Offset(0, 0).Value
'Debug.Print y
Cutoff = y
'Debug.Print Cutoff
End Function
가 엑셀에서 운영하고 실행하는 VBA.
Sub Export2()
Dim db2 As Database
Dim rs2 As DAO.Recordset, i As Long, sFormat As String
Dim WB2 As Excel.Workbook, WS2 As Excel.Worksheet
Set WB2 = Workbooks.Open("C:\FilePath.z.xlsm")
Set WS2 = WB.Sheets("Sheet2")
Set db2 = OpenDatabase("C:\FilePath.x.mdb")
Set qd2 = db2.QueryDefs("ExportCount")
Set rs2 = qd2.OpenRecordset()
If rs2.EOF Then
GoTo EndLoop
End If
WS2.Range("a1").End(xlsDown).Offset(1, 0).CopyFromRecordset rs2
WS2.Cells.EntireColumn.AutoFit: WS2.Cells.EntireRow.AutoFit
EndLoop:
Set WB = Nothing
Set WS2 = Nothing
Set db2 = Nothing
Set qd2 = Nothing
Set rs2 = Nothing
End Sub
편집 :
도 시도 해 봤나 :
Sub SQLquery1()
Dim WB1 As Excel.Workbook, WS1 As Excel.Worksheet
Dim wt As DAO.Database
Dim we As DAO.Recordset
Dim wd As DAO.QueryDef
Set WB1 = Workbooks.Open("C:\x.xlsm")
Set WS1 = WB1.Sheets("Sheet2")
mySQLVariable = WS1.Range("A1").End(xlDown).Offset(0, 0).Value
'Debug.Print mySQLVariable
Set wt = OpenDatabase("C:\z.mdb")
Set wd = wt.QueryDefs("ExportCount")
Set we = wd.OpenRecordset("h")
WS2.Range("a1").End(xlsDown).Offset(1, 0).CopyFromRecordset wd
WS2.Cells.EntireColumn.AutoFit: WS2.Cells.EntireRow.AutoFit
Set WB1 = Nothing
Set WS1 = Nothing
Set wt = Nothing
Set we = Nothing
Set wd = Nothing
End Sub
EDIT2
Sub CreateQueryDef()
Dim WB1 As Excel.Workbook, WS1 As Excel.Worksheet
Dim dbPP As Database
Dim qdfTemp As QueryDef
Dim Counter As DAO.Recordset
Dim mySQLVariable As String
Dim rs5 As DAO.Recordset
Set dbPP = OpenDatabase("C:\filepath\z.mdb")
Set Counter = dbPP.OpenRecordset("j")
Set WB1 = Workbooks.Open("C:\filepath\x.xlsm")
Set WS1 = WB1.Sheets("Sheet2")
mySQLVariable = WS1.Range("A1").End(xlDown).Offset(0, 0).Value
'Debug.Print mySQLVariable
With dbPP
Set qdfTemp = dbPP.CreateQueryDef("NewQueryDef", "SELECT * FROM [j]")
'WHERE ((j.[ID])=>(mySQLVariable)))") I can't get the syntax of these lines right - they are supposed to all be on the same line
Set rs5 = qdfTemp.OpenRecordset() ' maybe Set rs5 = qdfTemp.OpenRecordset("NewQueryDef")?
End With
WS1.Range("a1").End(xlsDown).Offset(1, 0).CopyFromRecordset rs5
WS1.Cells.EntireColumn.AutoFit: WS2.Cells.EntireRow.AutoFit
dbPP.QueryDefs.Delete "NewQueryDef"
End Sub
또는
나는 다음과 같은 시도Sub CreateQueryDef()
Dim dbPP As Database
Dim qdfTemp As QueryDef
Dim Counter As DAO.Recordset
Dim mySQLVariable As String
Dim rs5 As DAO.Recordset
Set dbPP = OpenDatabase("C:\filepath\z.mdb")
Set Counter = dbPP.OpenRecordset("j")
mySQLVariable = CutOff
'Debug.Print mySQLVariable
With dbPP
Set qdfTemp = dbPP.CreateQueryDef("NewQueryDef", "SELECT * FROM [j] WHERE ((j.[ID])=>(mySQLVariable)))")
Set rs5 = qdfTemp.OpenRecordset("NewQueryDef")
End With
WS1.Range("A1").End(xlsDown).Offset(1, 0).CopyFromRecordset rs5
WS1.Cells.EntireColumn.AutoFit: WS2.Cells.EntireRow.AutoFit
dbPP.QueryDefs.Delete "NewQueryDef"
dbPP.Close
Set dbPP = Nothing
Set qdfTemp = Nothing
Set Counter = Nothing
Set mySQLVariable = Nothing
Set rs5 = Nothing
End Sub
Public Function Cutoff()
Dim WB1 As Excel.Workbook, WS1 As Excel.Worksheet
Dim y As Long
Set WB1 = Workbooks.Open("C:\filepath.z.xlsm")
Set WS1 = WB1.Sheets("Sheet2")
y = WS1.Range("A1").End(xlDown).Offset(0, 0).Value
'Debug.Print y
Cutoff = y
'Debug.Print Cutoff
End Function
) cutoff 함수는 데이터베이스와 Excel 스프레드 시트, 스프레드 시트 (MDB가 어떻게 찾을 수 있습니까?) 또는 데이터베이스에만 존재합니까? Excel에서 Excel의 경우 컷오프 값이 포함 된 exportcount의 매개 변수를 허용하도록 액세스에서 쿼리를 수정해야합니다. – xQbert
감사합니다. 그러나 문제가 발생한 경우를 대비하여 Excel과 Access 모두에 기능을 추가했습니다. 원래 나는 그것을 액세스에 설정하고, 말하자면, 액세스에서 실행되고 쿼리 결과가 올바르게 표시 될 때 올바른 값을 유도합니다. 쿼리는 excel vba에서 호출 될 때 문제가 발생합니다. – Orphid
아마도 Excel에서 CutOff 함수를 호출하는 SQL 쿼리에서 mySQLVariable (EDIT 2)을 변경해야합니까? 또는 쿼리를 시작하기 전에 CutOff로 mySQLVariable을 정의 할 수 있습니까? – Orphid