a. Code for connecting to the excel file
Sub connectivity()
On Error GoTo myerr:
Set cn1 = New ADODB.Connection
With cn1
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source='" & ThisWorkbook.FullName & "'; Extended Properties=Excel 8.0;"
.Open
End With
myerr:
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub
B. Code for reading the datas from the excel file
Sub readingrecord()
connectivity
Set rs = New ADODB.Recordset
rs.Open "select * from [sheet1$]", cn1, adOpenStatic, adLockOptimistic
If rs.EOF = False Then
rs.MoveFirst
Do While Not rs.EOF
myName = rs.Fields(1).Value & "," & rs.Fields(2).Value
Set rs1 = New ADODB.Recordset
rs1.Open "select * from [sheet1$] where [Namefield] ='" & myName & "'", cn1, adOpenStatic, adLockOptimistic
If rs1.EOF = True Then
Sheet7.Cells(i, 1) = myName
Sheet7.Cells(i, 2) = 0
i = i + 1
End If
rs.MoveNext
Loop
End If
End sub
C. Code for save the data to the external excel file.
Sub fileupload()
Sheets("sheet1").Select
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:= _
"C:\test.xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "sheet1"
Range("C8").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
Range("A1").Select
End Sub
No comments:
Post a Comment