【问题标题】:Excel VBA Passing a range of cells in column A to next columns with different range sizeExcel VBA将A列中的一系列单元格传递给具有不同范围大小的下一列
【发布时间】:2021-10-12 12:46:48
【问题描述】:

我在 A 列中有一个单词列表,因此每个单词都在一个单元格中。 我需要计算指定范围的熵,所以我需要在B列范围A1:A10,在C列范围A1:A20,在D列范围A1:A30 ... A 列中的列数和单元格数总是不同的,因为每次都需要不同的文本。 为此,我需要有关 VBA 代码的帮助。 谢谢。

【问题讨论】:

  • 你自己尝试了什么?你打算用各自的范围做什么?我可以告诉你,但是如何使用它们,你的工作就是解释......

标签: excel vba


【解决方案1】:

考虑:

Sub FillUp()
    Dim items As Long, times As Long
    
    items = Cells(Rows.Count, "A").End(xlUp).Row
    times = items / 10
    
    For i = 1 To times
        j = i * 10
        Range(Cells(1, 1), Cells(j, 1)).Copy Range(Cells(1, i + 1), Cells(j, i + 1))
    Next i
End Sub

这假设列 A 中的项目数是 10

的偶数倍

【讨论】:

  • 非常感谢!
【解决方案2】:

在 Gary 的学生答案之上添加并稍微更改代码,这也适用于不是 10 的倍数的项目数。

“test2”是我使用的工作表的代号。

Sub FillUp()

    Dim items As Long, times As Long

    items = test2.Range("A1").CurrentRegion.Rows.Count
    times = items / 10

    Dim i As Long

    For i = 1 To times

        test2.Range("A1").Resize(10 * i, 1).Copy test2.Range("A1").Offset(0, i)
    
    Next i

    If items Mod 10 <> 0 And items > 10 Then
    
        test2.Range("A1").Resize(items, 1).Copy test2.Range("A1").Offset(0, i)
    
    End If

End Sub

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多