在Excel中组合两列并使用VBA将Access导出为一列

时间:2014-09-30 13:30:16

标签: excel vba access-vba

我需要一些帮助。我正在尝试构建一个我希望根据日期/时间排序的Access表。我从Excel工作表导出此数据。日期在一个单元格中,但时间在一列中。访问表中的列是日期,时间,储罐和注释。我希望Date列看起来像“mm / dd / yy hhmm”。导出Date时,我希望在循环的每次运行中包含Time。代码段的一部分如下所示:.Fields("Date") = Range("B" & d "and "A" & r").Value,其中“A”& r是时间列,其中r是行号,我该怎么编程呢?谢谢。

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 个答案:

答案 0 :(得分:1)

您是否尝试过如下的连接?

.Fields("Date") = Range("B" & d).Value & " " & Range("A" & r).Value
相关问题