将数组复制到单元格

发布于 2024-12-11 14:58:41 字数 349 浏览 0 评论 0原文

我目前有一个充满数据的数组,我想将其一直传输到单元格 G2,直到整个数组耗尽。我当前的电子表格在 G1 中有数据,但在其下没有数据。

我有以下代码,但它并不完全有效,因为我收到错误:

应用程序定义或对象定义的错误。

帮助解决这个问题就太好了。如果您能告诉我我的代码有什么问题以及如何修复它,而不是为我提供替代方案,我将不胜感激。

For i = 1 to nFlights
     With Worksheets("Q2").Range("G1")
           .End(xlDown).Offset(1, 0) = Origin(i)
    End With
Next

I currently have an array full of data that I would like to transfer to cell G2 all the way until the entire array is exhausted. My current spreadsheet has data in G1 but no data under that.

I have the following code but it doesn't exactly work because I get the error:

Application-defined or object-defined error.

Help fixing this problem would be great. I would appreciate if you could tell me what is wrong with my code and how to fix it rather than providing me with an alternative formulation.

For i = 1 to nFlights
     With Worksheets("Q2").Range("G1")
           .End(xlDown).Offset(1, 0) = Origin(i)
    End With
Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-12-18 14:58:41

我知道您不想显示代码,但这是与您的循环不同的方法:

Dim arr
arr = Array("one","two","three","four")

ActiveSheet.Range("G2").Resize((UBound(arr) - LBound(arr)) + 1, 1).Value = _
                                  Application.Transpose(arr)

I know you didn't want to be shown code, but here's a different approach from your loop:

Dim arr
arr = Array("one","two","three","four")

ActiveSheet.Range("G2").Resize((UBound(arr) - LBound(arr)) + 1, 1).Value = _
                                  Application.Transpose(arr)
你的笑 2024-12-18 14:58:41

offset 属性仅引用单元格(在您的情况下),该单元格位于作为调用基础的单元格(“G1”)下方。为了将整个数组原点复制到工作表上的 G 列中,请尝试以下操作:

Dim i As Integer
i = 2

For Each val As String In Origin
    With Worksheets("Q2")
       .Cells(i, 7).Value = Origin(i)
       i = i + 1
    End With
Next

The offset property just references the cell (in your case), one below the cell that is the base of the call ("G1"). In order to copy the whole array orgin into the column G on the sheet try this:

Dim i As Integer
i = 2

For Each val As String In Origin
    With Worksheets("Q2")
       .Cells(i, 7).Value = Origin(i)
       i = i + 1
    End With
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文