【问题标题】:excel copy cell from a closed workbook.excel 从已关闭的工作簿中复制单元格。
【发布时间】:2013-11-14 15:36:34
【问题描述】:

我正在尝试将已关闭工作簿中的单元格中的数据复制到另一个工作簿中,而无需打开原始工作簿。

我已经展示的代码...有效,但我需要能够将数据写入打开的工作簿上的某个单元格。

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

Sub TestGetValue2()
    p = "F:\excel_Project"
    f = "Book1.xlsx"
    s = "Sheet1"
    a = "A1"
 GetValue(p, f, s, a) = ("A1")

End Sub

【问题讨论】:

    标签: excel vbscript excel-2007 xlm vba


    【解决方案1】:

    如果您想将单元格的值设置为 GetValue 函数返回的任何值,您可以执行以下操作

    Range("A1") = GetValue(p, f, s, a)
    

    但是,您需要确保您的目的地(即您要写入GetValue 的位置)与GetValue 的大小/尺寸相同。例如,如果GetValue 是一个单元格,那么上述将按预期工作。但是,如果 GetValue 是 1x2 范围的单元格并且您使用上述代码,则只有 A1 将填充来自 GetValue 的值(第一个值)

    【讨论】:

    • + 1 获取更多信息
    【解决方案2】:

    我需要从存储在工作表单元格中的已关闭文件的动态列表中检索一个单元格。

    除了循环,我尝试使用公式来解决我的问题

    这对我有用:

    Range("A1").Formula ="='F:\excel_Project\[Book1.xlsb]Sheet1'!$A1"
    

    如果有人需要对文件列表做同样的事情......

    Range("a" & index).Formula = "='f:\excel_project\[" & myname & ".xlsb]Sheet1'!$A1"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多