2015-12-02 100 views
2

我正在为一个工作项目工作,我碰到了一堵墙。我正试图自动化一些格式来加速进程。在Sheet1上,有一个G2到W21的表格。包含在此表中的数据由用户通过用户窗体输入。数据输入后,我使用这些数据驱动Sheet2格式化。到目前为止,我已经想出了如何处理这张表的列G & H我想要的方式。我无法弄清楚如何处理列I:M和O:W。Excel VBA:显示数组?

下面是代码,我拿出这么远:

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

+0

你能告诉什么是Sheet2的此刻看起来像一张照片? –

+1

看起来您正在超越电子表格的正常使用。你问的问题可以很容易地处理,但是随着时间的推移,我认为你会希望你已经在数据库应用程序或数据库支持的内部网上完成了这个任务。考虑一下需要添加什么东西或将工作簿合并以获得年度数字的道路。只是一个想法... – Beengie

+0

@ScottCraner我刚刚添加了几张图片 –

回答

1

没有理由使用数组的照片。范围是数组的本质。

这应该做你想要什么:

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 
+0

非常感谢,这工作完美。不知道为什么我试图让这么难。 –

+0

@GrahamChandler很高兴我能帮上忙。如果路线较短,我们都会陷入下面的线程中。 –

+0

你说得对! –