2013-11-14 40 views
0

我試圖將數據從一個封閉的工作簿中的單元格複製到另一個工作簿,而無需打開原來的工作簿。擅長從封閉的工作簿複製單元格。

我的代碼顯示遠...工作,但我需要能夠將數據寫入打開的工作簿上的某個單元格。

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 

回答

1

如果你想設置爲任何你GetValue函數返回一個單元格的值,你會做以下

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

但是,你需要確保你的目的地(即你想寫的地方GetValue)與GetValue的尺寸/尺寸相同。例如,如果GetValue是一個單元,那麼上述將按預期工作。但是,如果GetValue是單元格的1x2範圍,並且您使用上述代碼,則只有A1將填充值爲GetValue

+0

+ 1的值(第一個值)對於其他信息 –

相關問題