1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| Sub FromExcelToAccess()
Sheets("Measures").Select ' Call Sheet Name
'Exports data from the active worksheet to a table in an Access database
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' Connect to the Access database
Set cn = New ADODB.Connection
' Pilote connection definition
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
'The Database path
cn.ConnectionString = "G:\My Documents\graphs\Silos.mdb"
'Open Database
cn.Open
' Open a recordset
Set rs = New ADODB.Recordset
rs.Open "Group1", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' All records in a table
r = 7 ' The start row in the worksheet
Do While r < 24
' Repeat until complete
With rs
.AddNew 'Create a new record
' Add values to each field in the record
' Check Date to Time / This handles all Data Tables
.Fields("Group1_Date") = Format((Range("B1").Value), "Short Date")
' Begin the retrival of the Group1 Data
.Fields("Group1_Silo") = Round(Range("A" & r).Value + "0", 2)
.Fields("Group1_Hac_Code") = Range("B" & r).Value ' This handles all Data Tables
.Fields("Group1_Type") = Range("C" & r).Value
.Fields("Group1_Density") = Round(Range("D" & r).Value + "0", 2)
.Fields("Group1_Start_Time") = Format(Range("E6").Value, "Medium Time")
.Fields("Group1_Start_Measure") = Round(Range("E" & r).Value + "0", 2) ' Force zero entry when null
.Fields("Group1_Start_Volume") = Round(Range("I" & r).Value + "0", 2)
.Fields("Group1_Start_Tonnage") = Round(Range("M" & r).Value + "0", 2)
.Fields("Group1_1st_Time") = Format(Range("F6").Value, "Medium Time")
.Fields("Group1_1st_Measure") = Round(Range("F" & r).Value + "0", 2)
.Fields("Group1_1st_Volume") = Round(Range("J" & r).Value + "0", 2)
.Fields("Group1_1st_Tonnage") = Round(Range("N" & r).Value + "0", 2)
.Fields("Group1_2nd_Time") = Format(Range("G6").Value, "Medium Time")
.Fields("Group1_2nd_Measure") = Round(Range("G" & r).Value + "0", 2)
.Fields("Group1_2nd_Volume") = Round(Range("K" & r).Value + "0", 2)
.Fields("Group1_2nd_Tonnage") = Round(Range("O" & r).Value + "0", 2)
.Fields("Group1_3rd_Time") = Format(Range("H6").Value, "Medium Time")
.Fields("Group1_3rd_Measure") = Round(Range("H" & r).Value + "0", 2)
.Fields("Group1_3rd_Volume") = Round(Range("L" & r).Value + "0", 2)
.Fields("Group1_3rd_Tonnage") = Round(Range("P" & r).Value + "0", 2)
r = r + 1 'Next Row
End With
Loop
' Closing the database and clearing the rs object
r = 0
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
' Copy the last values of the last day in the start values of the new day
Sheets("Measures").Range("H7:H23").Copy
Range("E7:E23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' Change the date for new day
Sheets("Sheet1").Range("B11").Copy
Sheets("Measures").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub |
Partager