Hello Excel Forum
I want to add column headers to the first row in the array so how can I not lose their data
I want to add column headers to the first row in the array so how can I not lose their data
Code:
Sub RevisedLoadArray()
Dim DBA As Object, RES As Object, SQL As String
Dim Name_Sheet As String
Set DBA = CreateObject("ADODB.Connection")
Dim My_Array()
Dim Ar
Dim i, ii, C As Long
Set DBA = New ADODB.Connection
Set RES = New ADODB.Recordset
Nm = "aa.xls"
DBPath = "C:\Users\user\Desktop\" & Nm
With DBA
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & DBPath & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
Name_Sheet = "Tst"
SQL = "SELECT * FROM" & "[" & Name_Sheet & "$]" & ""
Set RES = DBA.Execute(SQL)
ReDim My_Array(1, 30)
For ii = 0 To RES.Fields.Count - 1
My_Array(1, C) = RES.Fields(ii).Name
C = C + 1
Next ii
Do While Not RES.EOF
C = C + 1
For i = 0 To RES.Fields.Count - 1
ii = ii + 1
My_Array(ii, C) = RES.Fields(i)
Next
RES.MoveNext
Loop
ReDim Preserve My_Array(UBound(Vluse, 2) + 1, UBound(Vluse, 1) + 1)
Range("A1").Resize(UBound(My_Array, 2), UBound(My_Array, 1)) = My_Array
RES.Close: Set RES = Nothing: Erase My_Array
DBA.Close: Set DBA = Nothing
End Sub
Last edited by a moderator: