NguyenDang95
Senior Member
Áp dụng cho: Excel 365, Excel 2021.
Một tệp Excel có thể thực hiện chức năng như một cơ sở dữ liệu để thực hiện nhiều thao tác trích lọc, truy vấn dữ liệu khác nhau. Ngoài Power Query (ngôn ngữ Power Query M), chúng ta có thể dùng SQL (trong trường hợp này là Access SQL) truy vấn trực tiếp đến tệp Excel, điều này thật sự hữu ích khi dữ liệu khá lớn và những công cụ có sẵn của Excel xử lý chậm hẳn.
Sử dụng hàm đã viết trong VBA:
Lưu ý: Do ô (Cell) trong Excel chỉ cho phép chứa tối đa 255 ký tự, cho nên đối với truy vấn SQL quá dài, chúng ta có thể chia nhỏ thành nhiều phần rồi dùng hàm Concatenate để nối lại thành chuỗi hoàn chỉnh.
Tìm hiểu thêm:
Một tệp Excel có thể thực hiện chức năng như một cơ sở dữ liệu để thực hiện nhiều thao tác trích lọc, truy vấn dữ liệu khác nhau. Ngoài Power Query (ngôn ngữ Power Query M), chúng ta có thể dùng SQL (trong trường hợp này là Access SQL) truy vấn trực tiếp đến tệp Excel, điều này thật sự hữu ích khi dữ liệu khá lớn và những công cụ có sẵn của Excel xử lý chậm hẳn.
Code:
Option Explicit
'Tham chiếu đến thư viện Microsoft ActiveX Data Object 6.1 Library.
Public Function EXECUTESQL(SQLStatement As String) As Variant
Dim objCnn As ADODB.Connection
Dim objCmd As ADODB.Command
Dim objRs As ADODB.Recordset
Dim arrResult() As Variant, arrMergedArray() As Variant, arrColumnNames() As Variant
Dim strCnn As String, strColumnName As String
Dim i As Long
EXECUTESQL = CVErr(xlErrNA)
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName & ";Extended Properties='Excel 12.0 Xml;HDR=YES';"
Set objCnn = New ADODB.Connection
With objCnn
.ConnectionString = strCnn
.Open
End With
Set objCmd = New ADODB.Command
With objCmd
.ActiveConnection = objCnn
.CommandText = SQLStatement
Set objRs = .Execute
End With
If Not objRs Is Nothing Then
On Error Resume Next
arrResult = Application.WorksheetFunction.Transpose(objRs.GetRows)
ReDim arrColumnNames(0 To objRs.Fields.Count - 1)
For i = 0 To objRs.Fields.Count - 1
arrColumnNames(i) = objRs.Fields.Item(i).Name
Next
arrMergedArray = PrepareOutputData(arrColumnNames, arrResult)
Erase arrColumnNames
Erase arrResult
If Err.Number <> 0 Then
EXECUTESQL = CVErr(xlErrNA)
Err.Clear
Exit Function
End If
End If
objRs.Close
objCnn.Close
EXECUTESQL = arrMergedArray
Set objCnn = Nothing
Set objCmd = Nothing
Set objRs = Nothing
End Function
Private Function PrepareOutputData(ColumnNames As Variant, TableData As Variant)
Dim arrResult() As Variant
Dim i As Long, j As Long
If Is2DArray(TableData) Then
ReDim arrResult(0 To UBound(TableData), 0 To UBound(ColumnNames))
For i = 0 To UBound(ColumnNames)
arrResult(0, i) = ColumnNames(i)
Next
For i = 1 To UBound(TableData)
For j = 0 To UBound(ColumnNames)
arrResult(i, j) = TableData(i, j + 1)
Next
Next
Else
ReDim arrResult(0 To 1, 0 To UBound(ColumnNames))
For i = 0 To UBound(ColumnNames)
arrResult(0, i) = ColumnNames(i)
Next
For j = 0 To UBound(ColumnNames)
arrResult(1, j) = TableData(j + 1)
Next
End If
PrepareOutputData = arrResult
End Function
Private Function Is2DArray(InputArray As Variant) As Boolean
Dim lngMaxColumnIndex As Long
On Error Resume Next
lngMaxColumnIndex = UBound(InputArray, 2)
If Err.Number = 9 Then
Err.Clear
Is2DArray = False
Else: Is2DArray = True
End If
End Function
Sử dụng hàm đã viết trong VBA:
Lưu ý: Do ô (Cell) trong Excel chỉ cho phép chứa tối đa 255 ký tự, cho nên đối với truy vấn SQL quá dài, chúng ta có thể chia nhỏ thành nhiều phần rồi dùng hàm Concatenate để nối lại thành chuỗi hoàn chỉnh.
Tìm hiểu thêm:
Run SQL queries on Excel files (Power Automate)
https://docs.microsoft.com/en-us/power-automate/desktop-flows/how-to/sql-queries-excel
Last edited: