gpt4 book ai didi

excel - 将数组粘贴到范围仅粘贴第一个值

转载 作者:行者123 更新时间:2023-12-04 19:46:44 24 4
gpt4 key购买 nike

我正在尝试将数组值粘贴到一个范围内,但它只将第一个值粘贴到范围内。

Dim Array1()

For i = 0 to 9
ReDim Preserve Array1
Array1(i)= Int((6 * Rnd) + 1)
Next

ws.Range("A1").Value = Array1

输出:

enter image description here

我是不是做错了什么?

最佳答案

您必须正确调整数组的尺寸,然后转置它。

Option Explicit

Sub Sample()
Dim Array1()
Dim ws As Worksheet
Dim i As Long

'~~> Change to respective sheet
Set ws = Sheet1

For i = 0 To 9
ReDim Preserve Array1(i) '<~~ increment by i
Array1(i) = Int((6 * Rnd) + 1)
Next

'~~> Store in the worksheet
ws.Range("A1").Resize(UBound(Array1) + 1, 1).Value = Application.Transpose(Array1)
End Sub

关于excel - 将数组粘贴到范围仅粘贴第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70122871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com