Как создать пользовательские сочетания клавиш для функций скрипта приложения Google?

Подобное приложение, но не жестко закодированные пути, как в приведенных выше примерах. Эта функция копирует значение из другой закрытой книги, аналогичной функции = INDIRECT (), но не такой сложной. Это возвращает значение ... не ссылку, поэтому его нельзя использовать с дополнительными функциями, требующими ссылок (т. Е .: VLOOKUP ()). Вставьте этот код в новый модуль VBA:

'Requires filename, sheetname as first argument and cell reference as second argument
'Usage: type in an excel cell -> =getvalue(A1,B1)
'Example of A1 -> C:\TEMP\[FILE1.XLS]SHEET1'
'Example of B1 -> B3
'This will fetch contents of cell (B3) located in (sheet1) of (c:\temp\file1.xls)

'Create a module and paste the code into the module (e.g. Module1, Module2)

Public xlapp As Object

Public Function getvalue(ByVal filename As String, ref As String) As Variant

' Retrieves a value from a closed workbook
    Dim arg As String
    Dim path As String
    Dim file As String

    filename = Trim(filename)

    path = Mid(filename, 1, InStrRev(filename, "\"))
    file = Mid(filename, InStr(1, filename, "[") + 1, InStr(1, filename, "]") - InStr(1, filename, "[") - 1)

    If Dir(path & file) = "" Then
        getvalue = "File Not Found"
        Exit Function
    End If

    If xlapp Is Nothing Then
        'Object must be created only once and not at each function call
        Set xlapp = CreateObject("Excel.application")
    End If


    ' Create the argument
    arg = "'" & filename & "'!" & Range(ref).Range("A1").Address(, , xlR1C1)

    'Execute an XLM macro
    getvalue = xlapp.ExecuteExcel4Macro(arg)

End Function
45
задан Rubén 20 April 2018 в 16:40
поделиться