简体   繁体   English

如何在excel范围内显示1或2 dim varraint array()返回excel UDF函数而不使用Ctrl + Shift + Enter数组论坛方法

[英]How to display a 1 or 2 dim varraint array() return from an excel UDF function in an excel range without Ctrl+Shift+Enter array forumla method

I've searched for solutions to this question for 3 days & have only found answers that either use Ctrl + Shift + Enter (array formula method) or functions which either return an empty range or which raise err 1004. 我已经搜索了这个问题的解决方案3天了,并且只找到了使用Ctrl + Shift + Enter (数组公式方法)或者返回空范围或者提高错误1004的函数的答案。

I'm using 64-bit Windows 8.1 and Excel-2013 write UDFs that return variant arrays from time to time with unknown array size returns... for example 'MyFunction(args...) as Variant()'. 我正在使用64位Windows 8.1和Excel-2013编写UDF,这些UDF会不时返回变量数组,并返回未知数组大小...例如'MyFunction(args ...)作为Variant()'。 I can see the result in immediate window or write it to file or display it with Ctrl + Shift + Enter as an array formula. 我可以在即时窗口中看到结果或将其写入文件或使用Ctrl + Shift + Enter作为数组公式显示。

What I want to do is use MyFunction() as an argument to a sub arr2rng(Myfunction) such that arr2rng() fills a range on the activesheet starting at ActiveCell inorder to avoid the whole manual routine of using array formula method (eg highlight a range of some size larger, by guessing, than the returned array, then the combination Ctrl + Shift + Enter to display the array). 我想要做的是使用MyFunction()作为sub arr2rng(Myfunction)的参数,以便arr2rng()从ActiveCell开始填充活动表上的范围,以避免使用数组公式方法的整个手动例程(例如,突出显示a一些大小的范围,通过猜测,比返回的数组,然后组合Ctrl + Shift + Enter来显示数组)。

I've even tried the long subroutine by Nile -- see his A generic VBA Array To Range function at VBA Excel 2-Dimensional Arrays , 我甚至尝试过Nile的长子程序 - 在VBA Excel二维数组中查看他的通用VBA阵列到范围功能

Public Sub ArrayToRange(rngTarget As Excel.Range, InputArray As Variant)

but at every statement in 'ArrayToRange()' where 'rngOutput.Value2 = InputArray' occurs function bombs with err.Number 1004. Just before that statement is executed both 'InputArray' and 'rngOutput.VaAlue2' elements are correctly dimensioned and filled (from Immediate or Local Window observations)... though 'rngOutput.Value2'elments are still empty as they should be. 但是在'ArrayToRange()'中的每个语句中,其中'rngOutput.Value2 = InputArray'发生了具有err.Number 1004的函数炸弹。就执行该语句之前 ,'InputArray'和'rngOutput.VaAlue2'元素都被正确地标注和填充(从立即或本地窗口观察)...虽然'rngOutput.Value2'仍然是空的,因为它们应该是。 After the statement executes 'rgnOutput.value2' elements are still empty though, and err = 1004 has been raised. 语句执行'rgnOutput.value2'之后,元素仍然是空的,并且已经引发了err = 1004。 This occurs no matter which one of his tests in the code are executed. 无论代码中的哪个测试执行,都会发生这种情况。 I've even gone so far as to invoke his sub by my function at the end of my own VBA's as: 我甚至在我自己的VBA结束时通过我的功能调用了他的sub:

myFunction(args....) as Variant()
[do stuff...]
ConArr = vbaTransposeVar(ConArr)
Set DestCell = ACTIVE_CELL_DESTINATION
ArrayToRange DestCell, ConArr
myFunction = ConArr
End Function

where 'ConArr' is the name of the resultant variant() array and also the return from myFunction(), where 'ACTTIVE_CELL_DESTINATION' is declared Public as Range in the Module. 其中'ConArr'是结果variant()数组的名称,也是myFunction()的返回值,其中'ACTTIVE_CELL_DESTINATION'在模块中被声明为Public as Range。

What I prefer to do however is just invoke the 'sub ArrayToRange myDest, myArray' or any other sub such as a generic 'sub arr2rng(myDest as range, myArray() as Variant)' either from within some other function that invokes the sub or do it manually from the Macro window. 然而,我喜欢做的只是在调用sub的其他函数中调用'sub ArrayToRange myDest,myArray'或任何其他子类,例如泛型'sub arr2rng(myDest as range,myArray()作为Variant)'或者从“宏”窗口手动执行。

Can anybody help or tell me why all I get are either an empty range of cells or the 1004 error? 任何人都可以帮助或告诉我为什么我得到的是一系列空单元格或1004错误? I guess what I'm really asking is how to get around using the array formula method Ctrl + Shift + Enter . 我想我真正想问的是如何使用数组公式方法Ctrl + Shift + Enter There must be a way! 一定有办法! u ü

This example worked for me in testing, BUT there is no code to account for clearing any previous values which resulted from any earlier calculations. 这个例子对我来说在测试中起作用,但是没有代码可以解释任何先前计算产生的任何先前值。 If the current run results in a smaller array then previous values will not be cleared. 如果当前运行导致较小的数组,则不会清除先前的值。

You could account for this maybe by tracking the last range filled and making sure you clear it first, but depending on your exact use case that might not be viable. 您可以通过跟踪最后填充的范围并确保首先清除它来解决这个问题,但这取决于您可能不可行的确切用例。

在此输入图像描述

Function ChangeIt(func As Range, c1 As Range, c2 As Range)
    Dim arr(), r, c, rv, v

    For r = 1 To c1
    For c = 1 To c2
        v = "Row" & r & ":Col" & c
        If r = 1 And c = 1 Then
            rv = v 'return this to the calling cell via myArray
        Else
            'all other values are written directly
            func.Offset(r - 1, c - 1).Value = v
        End If
    Next c
    Next r

    ChangeIt = rv
End Function

'this is called from the worksheet
Function MyArray(arg1 As Range, arg2 As Range)

    Dim v

    v = arg1.Parent.Evaluate("Changeit(" & Application.Caller.Address(False, False) & "," & _
                                           arg1.Address(False, False) & "," & _
                                           arg2.Address(False, False) & ")")

    MyArray = v 'set the top-left array value

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA-用户定义函数-将与数组相关的公式(常规公式上为CTRL + SHIFT + ENTER)转换为UDF - Excel VBA - User Defined Function - converting array related formula (CTRL + SHIFT + ENTER on a normal formula) to a UDF 您是否可以在不按Ctrl + Shift + Enter的情况下向下复制数组公式? - Can you copy array formula down without having to hit Ctrl+Shift+Enter? Excel VBA Ctrl-Shift-Enter 数组 function 与编写长工作表相比非常慢 - Excel VBA Ctrl-Shift-Enter array function very slow compared to writing long worksheet function ctrl shift输入数组 - ctrl shift enter for array 获取Excel插件以修改数组公式参数; 或执行“ ctrl-shift-enter” - Getting Excel add ins to modify array formula parameters; or perform 'ctrl-shift-enter' 返回数组 Excel 内的范围 - Return the range within an array Excel 需要Excel Forumla对两个都包含数组公式的列进行排序 - Need Excel Forumla To Sort 2 Columns Which Both Contain Array Formulas Excel UDF接受范围和数组作为参数,例如“ SUM” - Excel UDF that accepts both Range and Array as Parameter like 'SUM' 使用UDF Excel 2007 VBA的2-DRange->数组-> 2D-Range - 2-DRange -> Array -> 2D-Range with UDF Excel 2007 VBA 如何返回 Excel VBA Function 中的记录数组? - How to return an array of records in Excel VBA Function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM