简体   繁体   English

VBA 代码将范围粘贴到 Excel 作为数组?

[英]VBA code to paste a range into Excel as an array?

So I have a range of items plugged in an Excel spreadsheet and I am looking for some code to paste those same values as an array (ie a ctrl + shift + enter formula) elsewhere in my spreadsheet.因此,我在 Excel 电子表格中插入了一系列项目,我正在寻找一些代码来将这些相同的值粘贴为我的电子表格中其他地方的数组(即 ctrl + shift + enter 公式)。 I've been trying to use the Range.FormulaArray property but can't seem to get it to work.我一直在尝试使用 Range.FormulaArray 属性,但似乎无法使其正常工作。

As an example, I have a list of text items such as:例如,我有一个文本项目列表,例如:

Apple苹果

Orange橙子

Banana香蕉

And I want to paste these items as a hardcoded array elsewhere in the spreadsheet so that the formula in cell A1 will read:我想将这些项目作为硬编码数组粘贴到电子表格的其他位置,以便单元格 A1 中的公式显示为:

={"Apple";"Orange";"Banana"} ={“苹果”;“橙”;“香蕉”}

And the values will spill in to the subsequent two cells并且这些值将溢出到随后的两个单元格

I'm using the latest version of Office 365.我正在使用最新版本的 Office 365。

Any ideas?有任何想法吗?

Use .Formula2 :使用.Formula2

Sub EasyAsOneTwoThree()
    Range("A1").Formula2 = "={""Apple"";""Orange"";""Banana""}"
End Sub

在此处输入图像描述

As you see, it spills down very nicely.如您所见,它很好地溢出。

(notice I had to double-up on the double quotes) (注意我必须在双引号上加倍)

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

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