gpt4 book ai didi

excel - 从关闭的 Excel 文件中获取公式(不仅仅是值)

转载 作者:行者123 更新时间:2023-12-02 05:42:53 25 4
gpt4 key购买 nike

我可以使用广泛使用的 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

最佳答案

为什么要写这么复杂的代码?由于某种原因,您正在使用的代码正在调用 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

关于excel - 从关闭的 Excel 文件中获取公式(不仅仅是值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7973507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com