arrays - Excel VBA : Displaying Arrays?

标签 arrays vba excel

我正在做一个工作项目,但我碰壁了。我正在尝试自动化一些格式化以加快进程。在 Sheet1 上,有一个 G2 到 W21 范围内的表格。此表中包含的数据由用户通过用户表单输入。数据输入后,我用这个数据驱动出Sheet2被格式化。到目前为止,我已经想出了如何以我想要的方式处理该表的 G 和 H 列。我不知道如何处理 I:M 和 O:W 列。

这是我到目前为止提出的代码:

Dim LineItems As Range, Cell As Range
Dim linearr() As Variant
Dim datasetarr() As Variant
Dim i As Integer
Dim j As Integer
Dim accountnum As Range
Dim accountnumrng As Range

Set LineItems = Sheet1.Range("H2:H21")
Set DataSets = Sheet1.Range("G2:G21")

For Each Cell In LineItems
    If Len(Cell.Value) > 0 Then
        i = i + 1
        ReDim Preserve linearr(1 To i)
        linearr(i) = Cell.Value
    End If
Next Cell

For Each Cell In DataSets
    If Len(Cell.Value) > 0 Then
        j = j + 1
        ReDim Preserve datasetarr(1 To j)
        datasetarr(j) = Cell.Value
    End If
Next Cell

Set accountnumrng = Sheet2.Range("B6:B1000").SpecialCells(xlCellTypeConstants, 23)

For Each accountnum In accountnumrng.Cells

accountnum.Offset(1, 1).Cells(1, 1).Resize(UBound(linearr), 1).Value = Application.Transpose(linearr)
accountnum.Offset(1, 0).Cells(1, 1).Resize(UBound(datasetarr), 1).Value = Application.Transpose(datasetarr)

Next accountnum

这是 Sheet1 上表格的图片。红色轮廓是我正在尝试使用的列

enter image description here

我基本上只是想扩展我到目前为止所了解的内容。任何帮助将不胜感激。

下面是 Sheet2 现在的样子
enter image description here

下面是我希望 Sheet2 的样子
enter image description here

最佳答案

没有理由使用数组。范围本质上是数组。

这应该做你想要的:

Dim accountnum As Range
Dim accountnumrng As Range
Dim lastrow As Long
Dim sze As Long

lastrow = Sheet1.Range("G2").End(xlDown).Row
sze = lastrow - 2 + 1

Set accountnumrng = Sheet2.Range("B6:B1000").SpecialCells(xlCellTypeConstants, 23)

For Each accountnum In accountnumrng.Cells
    accountnum.Offset(1, 8).Resize(sze, 9).Value = Sheet1.Range("O2:W" & lastrow).value
    accountnum.Offset(1, 0).Resize(sze, 7).Value = Sheet1.Range("G2:M" & lastrow).value
Next accountnum

关于arrays - Excel VBA : Displaying Arrays?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047212/

相关文章:

c - 使用 Typedef 将元素存储在二维数组中

javascript - 展平包含键\值的对象数组

javascript - 在 VBA 中从 Javascript 获取 XmlHttpRequest 的 ReadyState

sql-server - Powerpivot sql 查询失败 - 无法将值转换为表列请求的数据类型

excel - 如果范围内的任何文本与单元格内的文本匹配,则输出匹配的文本

excel - 阻止 Excel 计算单元格内容?

python - 让一个数字遍历数组中的 3 个数字

javascript - 这个递归数组置换函数是如何工作的?

excel - 从表中选择符合条件的数据,将其添加到用户窗体的组合框中

excel - Vlookup,excel,错误438 "object doesn' t支持此属性或方法”