【问题标题】:Access VBA to open, edit and save Excel doc访问 VBA 以打开、编辑和保存 Excel 文档
【发布时间】:2017-08-18 11:12:08
【问题描述】:

我有一个 Access DB 可以打开、编辑和保存一个 Excel 文档,该文档在第一次运行时运行良好,但如果我尝试更改多个文件(或同一文件两次),则会失败并显示“运行时错误” 1004': 对象 '_Global' 的方法 'Cells' 失败"

如果我关闭数据库然后重新打开它,它再次适用于第一个更改的文件。

虽然我对 VBA 并不陌生,但我会说我是新手。这是我正在使用的代码片段:

    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

【问题讨论】:

  • 在哪一行执行失败?
  • 如果 InStr(1, Cells(i, "C").Value, myVariable) > 0 那么
  • 坦率地说,我根本不知道它为什么会起作用。看起来此代码是从 Excel 复制的。在 Access 中,像 Cell 这样的直接调用不应该工作。使用 xl.Sheets("Sheet1").Cells(i, "C") 之类的变量
  • 谢谢谢尔盖。我会尝试你的建议,并在我的会议结束后发布结果。
  • 不要忘记下面行中的第二个Cells,也不要忘记使用Workbook 对象上的.Save 方法保存文件。

标签: ms-access vba


【解决方案1】:

应修改在 Access 中使用的 Excel VBA 代码。您不能直接调用 Excel 库方法,例如 Cell。为 Excel.Application、Workbook 和 Worksheet 声明变量,并将它们用于引用工作表单元格。避免使用激活方法。因此,在您的情况下,代码将如下所示:

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim lRowCount As Long

Dim myRange As Excel.Range

Set xl = CreateObject("Excel.Application")
strInputFile = varItem
Set wb = xl.Workbooks.Open(strInputFile)
Set ws = wb.Sheets("Sheet1")

lRowCount = ws.UsedRange.Rows.Count
'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 lRowCount
            If InStr(1, ws.Cells(i, "C").Value, myVariable) > 0 Then
                ws.Cells(i, "B") = "New Value"
                j = j + 1
            End If
        Next
    End If
End If

wb.Save
'Clean up
xl.Quit
Set xl = Nothing

别忘了添加对 Microsoft Excel 库的引用

【讨论】:

  • 再次感谢谢尔盖!!只需稍作改动即可完美运行。我不得不将 () 放在 Set wb = xl.Workbooks.Open (strInputFile) 中的变量周围。没什么大不了的,但想为这篇文章的潜在未来观众提一下。我还点击了这个答案的向上箭头,但由于我是这个网站的新手,显然我的选票还没有计算在内。 :-)
  • 是的,这里当然需要 (),我编辑了答案。如果答案对您有用,请接受,投票计数器下方会出现一个绿色复选标记
猜你喜欢
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多