我正在做一个工作项目,但我碰壁了。我正在尝试自动化一些格式化以加快进程。在 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 上表格的图片。红色轮廓是我正在尝试使用的列
我基本上只是想扩展我到目前为止所了解的内容。任何帮助将不胜感激。
下面是 Sheet2 现在的样子
下面是我希望 Sheet2 的样子
最佳答案
没有理由使用数组。范围本质上是数组。
这应该做你想要的:
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/