2013-01-21 56 views
0

我試圖在Access 2003中運行一個需要Excel文件的模塊,如果單元格中有信息,然後複製A1,B1等字段標題和單元格......它循環遍歷每一行。單元格中還有需要複製數據的註釋。從Access 2003 VBA排序/操縱Excel數據

因此,在一個完美的場景中,代碼會遍歷每行,逐個單元格,並抓取用戶,日期,單元格的數據和註釋,並在現有訪問表中創建新記錄。如果該單元格是空的,那麼它將被繞過。我有4張工作表,我知道如何讓代碼循環,我根本無法訪問從excel中拉出。

我不想只導入數據,它需要按照機智評論排序。

任何意見將不勝感激。謝謝!

下面是最接近我想我已經得到了BTW:

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 
+2

有點像? http://stackoverflow.com/questions/14285106/formatting-tables-imported-via-a-report/14285559#14285559 – Fionnuala

+0

這是非常接近我在找什麼,是的。但不是讓它逐行移動,我試圖讓它逐個單元格地移動,並用數據挑出單元格。 .Rows.Count能做到這一點嗎?我正在閱讀它,但只理解它的一部分......'如果rng.Cells(i,1)像「Program *」然後'做什麼?我熟悉If Then語句,但我從未使用過Like。謝謝 – Grant

+0

沒關係,我想通了。我只需要更多地混淆它。但是爲了移動逐個單元格以檢查是否爲空值或包含值,我應該使用一個在到達每行結尾後重置爲1的整數嗎? – Grant

回答

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