我正在使用以下代码将工作表解析为Access:
Sub LoadRates(ByRef TimesheetFile As Excel.Workbook)
On Error GoTo LoadDataCollection_Error
Dim i As Integer
Dim LastRow As Integer
Dim shRates As Excel.Worksheet
Set shRates = TimesheetFile.Worksheets("Rates")
shRates.ShowAllData
LastRow = shRates.Cells(shRates.Rows.Count, 1).End(xlUp).Row
shRates.Cells(1, 4).value = "Current"
Dim db As DAO.Database
Set db = CurrentDb
Dim strSQL As String
Dim dbWb As String
dbWb = "[Excel 12.0;HDR=YES;IMEX=1;Database=" & TimesheetFile.FullName & "].[Rates$A1:i" & LastRow & "]"
strSQL = "SELECT A.[Entity no] AS Entity,Staff AS Name, A.Current as Rates, Company, [2015 BCTC category] AS BCTC_Category,[2015 Rating] As Rating " & _
" INTO fromTimesheet " & _
" FROM " & dbWb & " AS A "
db.Execute strSQL, dbFailOnError
Exit Sub
LoadDataCollection_Error:
... do stuff...
End Sub
我的问题是我必须手动将第4列的标题更改为“当前”,因为标题当前是日期值,因此不适合SQL拿起
shRates.Cells(1, 4).value = "Current"
当我执行此代码时。有时它会返回一条错误消息,说它无法找到字段A.Current,有时它会找到它。这是由工作簿设置为共享引起的吗?
任何帮助将不胜感激。
答案 0 :(得分:0)
经过测试,我发现此错误与excel工作簿正在共享"。一旦我将工作簿排除在外,代码就能够找到我编辑行值的代码。
这使我相信当我实例化正在共享的excel工作簿对象时。它应该被视为只读。
希望这对其他人有帮助。
答案 1 :(得分:0)
是的,您的列标题未得到有效保存,并且连接正在使用上次保存的实例。
在进行更改后,请考虑手动保存。此外,请确保您具有保存工作簿的写入权限,而不是共享/只读(或者保存不同的临时工作簿以进行导入):
shRates.Cells(1, 4).value = "Current"
TimesheetFile.Save
但也考虑使用DoCmd.TransferSpreadshet方法而不是工作表连接(这可以促进“共享”过程)。通常一个人使用真正的数据库进行连接,而不是像电子表格那样使用平面文件。
Sub LoadRates(ByRef TimesheetFile As Excel.Workbook)
On Error GoTo LoadDataCollection_Error
Dim i As Integer, LastRow As Integer
Dim shRates As Excel.Worksheet
Set shRates = TimesheetFile.Worksheets("Rates")
shRates.ShowAllData
LastRow = shRates.Cells(shRates.Rows.Count, 1).End(xlUp).Row
shRates.Cells(1, 4).value = "Current"
TimesheetFile.Close True ' SAVES AND CLOSES
DoCmd.TransferSpreadsheet acImport, "fromTimesheet", _
TimesheetFile.FullName, True, "Rates!"
Exit Sub
LoadDataCollection_Error:
... do stuff...
End Sub