【问题标题】:Grab formulas from closed Excel file (not just values)从关闭的 Excel 文件中获取公式(不仅仅是值)
【发布时间】:2011-11-01 21:32:46
【问题描述】:

我可以使用广泛使用的 GetValues 函数从已关闭的工作簿中获取值;效果很好。

但有时我需要从已关闭的工作簿中获取单元格的公式。我尝试修改 GetValues 以获取单元格公式,但出现错误。

如何从已关闭的 excel 文件中获取单元格的公式(不是简单值)?

With Sheets
  For r = 2 To NewRowQty ' from second row to last row
    For c = 1 To ThisColumnEnd ' out to EndColumn (from import dialogue box)
      ThisCell = Cells(r, c).Address
      ThisValue = GetValue(ThisPath, ThisFile, ThisSheet, ThisCell)
      If ThisValue <> "0" Then
        If c = 3 And r > 2 Then
          Cells(r, c).Formula = GetFormula(ThisPath, ThisFile, ThisSheet, ThisCell)
        Else
          Cells(r, c) = ThisValue
        End If
      End If
    Next c
  Next r
End With

调用这两个函数,GetValue 工作正常,GetFormula 不会抓取公式。

Private Function GetValue(p, f, s, c)
  'p: path: The drive and path to the closed file (e.g., "d:\files")
  'f: file: The workbook name (e.g., "budget.xls")
  's: sheet: The worksheet name (e.g., "Sheet1")
  'c: cell: The cell reference (e.g., "C4")

  'Retrieves a value from a closed workbook
  Dim arg As String
  'Make sure the file exists
  If Right(p, 1) <> "\" Then p = p & "\"
  If Dir(p & f) = "" Then
    GetValue = "File Not Found"
    Exit Function
  End If
  'Create the argument
  arg = "'" & p & "[" & f & "]" & s & "'!" & _
  Range(c).Range("A1").Address(, , xlR1C1)
  'Execute an XLM macro
  GetValue = ExecuteExcel4Macro(arg)
End Function

Private Function GetFormula(p, f, s, c)
  'p: path: The drive and path to the closed file (e.g., "d:\files")
  'f: file: The workbook name (e.g., "budget.xls")
  's: sheet: The worksheet name (e.g., "Sheet1")
  'c: cell: The cell reference (e.g., "C4")

  'Retrieves a value from a closed workbook
  Dim arg As String
  'Make sure the file exists
  If Right(p, 1) <> "\" Then p = p & "\"
  If Dir(p & f) = "" Then
    GetFormula = "File Not Found"
    Exit Function
  End If
  'Create the argument
  arg = "'" & p & "[" & f & "]" & s & "'!" & _
  Range(c).Range("A1").Address(, , xlR1C1).Formula
  'Execute an XLM macro
  GetFormula = ExecuteExcel4Macro(arg)
End Function

更新:Joel 的第一个代码帖子是我最终使用的基础,因此我将其标记为正确。这是我使用整个行公式的复制粘贴的实际实现。这是最好的,因为我不知道有多少列可能包含值或公式,可能是 C 或 ZZ。

' silent opening of old file:
Application.EnableEvents = False
Set o = GetObject(FileTextBox.Text)
With Sheets
    For r = 2 To NewRowQty ' from second row to last row
        ThisCell = "A" & r
        o.Worksheets(ThisRate).Range(ThisCell).EntireRow.Copy
        Sheets(ThisRate).Range(ThisCell).PasteSpecial xlFormulas
    Next r
End With
' Close external workbook, don't leave open for extended periods
Set o = Nothing
Application.EnableEvents = True

【问题讨论】:

  • 您可以通过在后台打开工作簿获得更多控制权,这可以通过 vbscript 来完成,而不仅仅是 vba。加上循环通过 UDF 来检索值对于一个体面的数据抓取来说会非常慢。请参阅stackoverflow.com/q/7524064/641067,了解从关闭的工作簿访问数据的不同方法。根据 Tim 的评论,您可以禁用事件以停止任何工作簿事件(无论何时运行 codee,这都是一种很好的做法)

标签: excel vba excel-4.0 xlm


【解决方案1】:

为什么会有如此复杂的代码?由于某种原因,您正在使用的代码正在调用 Excel 4.0 向后兼容模式宏处理器。我无法想象你为什么要这样做。

这是从 c:\tmp\book.xlsx 的单元格 Sheet1!A1 中获取公式的简单方法:

Dim o As Excel.Workbook
Set o = GetObject("c:\tmp\Book.xlsx")
MsgBox o.Worksheets("Sheet1").Cells(1, 1).Formula
Set o = Nothing ' this ensures that the workbook is closed immediately

【讨论】:

  • 复杂的代码是因为它是更大数据导入器的一部分,从我们使用的早期版本的工具包中引入值和公式。这些外部文件有 Workbook_Activate 宏,会与当前文件混淆并呈现糟糕的用户体验;因此为什么我需要从已关闭的工作簿中获取数据(因此是问题)。
  • 我尝试了您的代码,但它启动了宏,但无法正常工作。 ExecuteExcel4Macro 实际上在现代书籍和网站中被提到是一个老东西,但它是从封闭的工作簿中获取值的唯一好东西,但我不知道从封闭的文件中获取公式。还有其他不打开外部工作簿的想法吗?
  • @Kirk - 您是否考虑过在打开文件之前关闭事件?
  • 你去吧,我不知道我可以关闭外部文件上的 Application.Events,所以我努力从关闭的文件中偷偷摸摸。我能够将这部分的代码减少一半,非常感谢,我从中学到了很多。
【解决方案2】:

如果您坚持运行Excel 4 - 风格的宏(1994 年已过时!),您需要使用XLM 函数GET.FORMULA 来检索公式而不是值,如下所示:

arg = "GET.FORMULA('" & p & "[" & f & "]" & s & "'!" & _
      Range(c).Range("A1").Address(, , xlR1C1) & ")"

请注意,结果将包含使用R1C1 表示法而不是A1 表示法的公式。

转换回A1 表示法(如果你真的想这样做的话)留给读者作为练习。

【讨论】:

    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多