简体   繁体   English

使用外接程序用户定义函数(UDF)的数据填充Excel多个单元格

[英]Populating Excel multiple cells with data from Add-in user defined function (UDF)

I am working on an Add-in for Excel that needs to populate one or more cells in the worksheet with the data from server. 我正在开发一个Excel加载项,该加载项需要使用服务器中的数据填充工作表中的一个或多个单元格。 I did following: 我做了以下事情:

  1. Created an Add-in (xlam) and created the user defined function: GetMyData() 创建了一个加载项(xlam)并创建了用户定义的函数:GetMyData()
  2. I am calling this function from a simple Excel Worksheet. 我从一个简单的Excel工作表中调用此函数。 In A1 cell, I entered the formula =GetMyData() 在A1单元格中,我输入了公式= GetMyData()
  3. My server returns JSON Array. 我的服务器返回JSON数组。 I am able to parse the JSON and now trying to populate the A1 and the below rows with values I got from the server. 我能够解析JSON,现在尝试使用从服务器获取的值填充A1和以下行。 (in this case, I have 20 values, so I want to populate A1:A20 cells). (在这种情况下,我有20个值,因此我想填充A1:A20单元格)。

The issue is that according to Microsoft KB, a user defined function is able to change the value of the active cell only. 问题是根据Microsoft KB,用户定义的功能只能更改活动单元格的值。 https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel I also tried to run the code below that changes only one cell to a hardcoded value: https://support.microsoft.com/zh-cn/help/170787/description-of-limitations-of-custom-functions-in-excel我也尝试运行下面的代码,该代码仅将一个单元格更改为硬编码值:

ActiveWorkbook.Sheets("DataSheet").Cells(1, 1).Value = '12312'

Still - getting exception. 仍然-越来越例外。

Could any one assist with this issue? 有人可以协助解决这个问题吗?

Here is a simple example: 这是一个简单的示例:

Public Function cerial(d As Date)
    Dim bry(1 To 20, 1 To 1) As String
    For i = 1 To 20
        bry(i, 1) = CStr(d) & "_" & i
    Next i
    cerial = bry
End Function

It creates an array of 20 items. 它创建一个包含20个项目的数组。

There is a tiny trick to using it in the worksheet. 在工作表中使用它有一个小技巧。

Select, say, E1 through E20 . 选择从E1E20 Then click in the formula bar and enter the array formula: 然后在公式栏中单击并输入数组公式:

=cerial(TODAY())

在此处输入图片说明

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. 必须使用Ctrl + Shift + Enter输入 数组公式 ,而不仅仅是Enter键。 If this is done properly, the formula will appear enclosed in brackets in the Formula Bar. 如果正确完成此操作,则公式将显示在公式栏中的括号内。

NOTE: 注意:

In this example, the internal array bry() is two dimensional. 在此示例中,内部数组bry()是二维的。

Have the function return a vertical array. 让函数返回一个垂直数组。

For Example: 例如:

Function GetMyData() As Variant()
Dim test() As Variant
test = Array(1, 2, 3, 4)
GetMyData = Application.Transpose(test)

End Function

Then highlight all the cells you would ever need with the top as the active cell. 然后突出显示您需要的所有单元格,并在顶部将其作为活动单元格。

![在此处输入图片描述

Then put this formula in the formula bar: 然后将此公式放在编辑栏中:

=GetMyData()

And hit Ctrl-Shift-Enter instead of Enter to array enter the formula. 然后按Ctrl-Shift-Enter而不是Enter即可输入公式。

在此处输入图片说明

If you choose more than the array returns you will get errors. 如果选择的数组多于返回的数组,则会出错。


Or you can, using the Array return, use this to deal with error and enter normally, but it runs the function for each time it is put in a cell. 或者,您可以使用Array返回,使用它来处理错误并正常输入,但是每次将其放入单元格时它都会运行该函数。

Put this in the first cell: 将其放在第一个单元格中:

=IFERROR(INDEX(GetMyData(),ROW(1:1)),"")

Then copy/drag down till you get blanks 然后复制/向下拖动直到出现空白

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM