0

我正在尝试在 Access 2003 中运行一个模块,该模块需要一个 excel 文件,如果单元格中有信息,然后在 A1、B1 等中复制字段标题和单元格……因为它循环遍历每一行。单元格中也有需要复制数据的注释。

因此,在一个完美的场景中,代码将逐个单元格地遍历每一行,并获取用户、日期、单元格的数据和评论,并在已经存在的访问表中创建一条新记录。如果单元格是空的,那么它将被绕过。我有 4 张纸,我知道如何制作代码循环,我根本无法从 excel 中提取。

我也不想只导入数据,它需要用机智的评论进行排序。

任何建议将不胜感激。谢谢!

下面是我认为我得到的最接近的顺便说一句:

Sub copy3()

Dim rs2 As New ADODB.Recordset
Dim cnn2 As New ADODB.Connection
Dim cmd2 As New ADODB.Command

With cnn2
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=C:\FilePath.xls;" & "Extended Properties=Excel 8.0;"
.Open
End With

Set cmd2.ActiveConnection = cnn2
cmd2.CommandType = adCmdText
cmd2.CommandText = "SELECT * FROM [Jan-Mar$] Where [Name] IS NOT NULL"
rs2.Open cmd2

While Not rs2.EOF
...Not sure what to write here but I got a msgbox to appear...
rs2.MoveNext
Wend
4

1 回答 1

2

感谢 Remou 的建议和帮助。这最终对我有用:

Set rs = db.OpenRecordset("TestTable")
xl.Visible = False

Set ws = xl.Workbooks.Open("C:\file.xls").Sheets("Jan-Mar")
Set rng = ws.UsedRange

'Row 1 is all dates so start at Row 2
For i = 2 To rng.Rows.Count
    'Column A contains names so start at Column B
    For j = 2 To rng.Rows(i).Cells.Count
        If rng.Cells(i, j) = "" Or rng.Cells(1, j) = "Total" Then

    Else
        On Error Resume Next
        rs.AddNew
        rs!EmployeeName = rng.Cells(i, 1)
        rs!Date = rng.Cells(1, j)
        rs!PointValue = rng.Cells(i, j)
        str = rng.Cells(i, j).Comment.Text
        str = Right(str, Len(str) - 14)
        'MsgBox str
        rs!Comments = str
        rs.Update
    End If
Next
Next
Set rs = Nothing
Set rng = Nothing
ws.Parent.Close
xl.Quit
于 2013-01-23T23:31:02.470 回答