2014-09-30 30 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