2014-09-30 63 views
0

我需要一些幫助。我想構建一個Access表,我想根據日期/時間進行排序。我從Excel工作表導出這些數據。日期在一個單元格中,但時間在一列中。訪問表中的列是日期,時間,坦克和評論。我希望日期列看起來像「mm/dd/yy hhmm」。導出日期時,我想在循環的每次運行中包含時間。代碼片段的一部分看起來像:.Fields("Date") = Range("B" & d "and "A" & r").Value,其中「A」& r是時間列,其中r是行號,我該如何編程?謝謝。在Excel中合併兩列並使用VBA作爲一列導出到Access

Sub ExportU1() 

    Sheets("Plant 1 WP Day").Select 
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, d, r As Long 
     ' connect to the Access database 
     Set cn = New ADODB.Connection 
     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _ 
      "Data Source=U:\Night Sup\Production Report 2003 New Ver 5-28-10_KA.mdb;" 
     ' open a recordset 

     Set rs = New ADODB.Recordset 
     rs.Open "UnitOneRouting", cn, adOpenKeyset, adLockOptimistic, adCmdTable 

     d = 2 'row location of date 
     r = 13 ' the start of Time, Tank and Comments row in the worksheet 
     Do While Len(Range("A" & r).Formula) > 0 
     ' repeat until first empty cell in column A 
      With rs 
       .AddNew ' create a new record 
       ' add values to each field in the record 
       .Fields("Date") = Range("B" & d).Value 
       .Fields("Time") = Range("A" & r).Value 
       .Fields("Tank") = Range("C" & r).Value 
       .Fields("Comments") = Range("D" & r).Value 
       .Update ' stores the new record 
      End With 
      r = r + 1 ' next row 
     Loop 
     rs.Close 
     Set rs = Nothing 
     cn.Close 
     Set cn = Nothing 
    End Sub 

回答

1

您是否嘗試過像下面這樣的連接?

.Fields("Date") = Range("B" & d).Value & " " & Range("A" & r).Value 
+0

哦,你好!謝謝。這樣可行。 – Kish 2014-09-30 14:17:38