Dear Gurus,
I have following code to use other excel as data source, but seems the code didn't catch the value from the excel sheet. Please help to advice. Thank you very much.
The data in the Current Code List.xls are
Thanks for the help
I have following code to use other excel as data source, but seems the code didn't catch the value from the excel sheet. Please help to advice. Thank you very much.
Code:
Sub Excel_ADO()
Dim cN As ADODB.Connection '* Connection String
Dim RS As ADODB.Recordset '* Record Set
Dim sQuery As String '* Query String
Dim i1 As Long
Dim lMaxRow As Long '* Last Row in the Sheet
Dim iRevCol As Integer '*
Dim i3 As Integer
On Error GoTo ADO_ERROR
Set cN = New ADODB.Connection
cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\seageath\Documents\DBConn\Current Code List.xls;Extended Properties=Excel 8.0;Persist Security Info=False"
cN.ConnectionTimeout = 40
cN.Open
Set RS = New ADODB.Recordset
lMaxRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
iRevCol = 2
For i1 = 2 To lMaxRow
Application.StatusBar = i1
strQuery = "SELECT * FROM [Sheet1$A" & i1 & ":B" & i1 & "]"
RS.ActiveConnection = cN
RS.Source = strQuery
RS.Open
If RS.EOF = True And RS.BOF = True Then
GoTo TakeNextRecord
End If
RS.MoveFirst
Do Until RS.EOF = True
sName = Trim$(RS("Name").Value)
sAge = Trim$(RS("Age").Value)
' Do some operations
RS.MoveNext
Loop
TakeNextRecord:
If RS.State <> adStateClosed Then
RS.Close
End If
Next i1
If Not RS Is Nothing Then Set RS = Nothing
If Not cN Is Nothing Then Set cN = Nothing
ADO_ERROR:
If Err <> 0 Then
Debug.Assert Err = 0
MsgBox Err.Description
Resume Next
End If
End Sub
The data in the Current Code List.xls are
A B
1 Name Age
2 Max 6
3 Elmo 10
4 Oni 17
Thanks for the help