我有一个Access数据库来打开,编辑和保存Excel文档,第一次运行时工作正常,但如果我尝试修改多个文件(或同一文件两次)“运行时错误'1004'失败:对象'_Global'的方法'单元格'失败”访问VBA以打开,编辑和保存Excel文档
如果我关闭数据库然后重新打开它,它再次适用于第一个文件改变。
尽管我对VBA并不陌生,但我会说我是新手。这里是我正在使用的代码snippit:
Code:
'Open spreadsheet and make it visible
Set xl = CreateObject("Excel.Application")
strInputFile = varItem
xl.Workbooks.Open strInputFile
xl.Visible = True
'Trying to get row count here but not working yet
'Set myRange = xl.Sheets("Sheet1").Range("C:C")
'lRowCount = Excel.Application.WorksheetFunction.CountA("Sheet1").Range("C:C")
'lRowCount = xl.WorksheetFunction.CountA(Worksheets("Sheet1").Cells(C, C))
'Debug.Print lRowCount
'strMyRange = "C:C"
'lRowCount = xl.WorksheetFunction.CountA(strMyRange)
'Debug.Print lRowCount
'lRowCount = Excel.Application.WorksheetFunction.CountA(Workbooks(strInputFile).Sheets("Sheet1").Range("C:C"))
'Debug.Print lRowCount
'Make the changes
j = 0
If Left(strFile, 4) = "xxxx" Then
myPath = "\\a\path\for\xxxx"
If InStr(1, strFile, "IQ") Then
For i = 1 To 500 'Row count not working yet
If InStr(1, Cells(i, "C").Value, myVariable) > 0 Then
Cells(i, "B") = "New Value"
j = j + 1
End If
Next
End If
End If
'Clean up
xl.Quit
Set xl = Nothing
Set objInputFile = Nothing
在该行的执行失败? –
If InStr(1,Cells(i,“C”)。Value,myVariable)> 0 Then – JDH
坦率地说,我不知道它为什么可以工作。看起来这个代码是从Excel复制的。在像Cell这样的Access直接调用应该不起作用。使用变量,如'xl.Sheets(“Sheet1”)。Cells(i,“C”)' –