2016-11-06 132 views
0

我对VBA很陌生,试图整合一个有20万行的excel文件。VBA宏将列数据添加到最后一行

我的Excel格式是

enter image description here

你可以看到这么多的重复数据存在。由于标题相同,我希望通过附加其他列数据来使整个工作表只有4列。

例如:

Data in the columns F2-I2 will go to A4 - D4 

        F3 - I3 will go to A5 - D5 

similarly 

        K2 - N2 will go to A6 - D6 

        K3 - N3 will go to A7 - D7 

,并遵循

我想写一个VBA宏,并运行它应该使Excel的宏通过附加其他所有列的数据只有4列。

有人可以帮助我实现这一目标。

注意:这样做后,我的Excel将有大约100万行。所以我正在寻找一个性能良好的解决方案。

回答

0

我认为这只能通过遍历块,列和行来解决,如果你有一百万个值,可能需要一些时间。

要提高性能禁用screenUpdating并确保宏不会崩溃包括循环中的DoEvents()。

请注意,在Excel中的最大行数是1'048'576

Sub moveValues() 

Application.ScreenUpdating = False 
Dim i As Integer 
Dim j As Integer 
Dim k As Long 
Dim row As Long 
Dim col As Integer 
Dim lastRowTarget As Long 
Dim lastRowSource As Long 

Dim wks As Worksheet 

Set wks = Sheets("Sheet1") 

For i = 6 To 16 Step 5 
    lastRowSource = wks.Cells(Rows.Count, Chr(64 + i)).End(xlUp).row 
    lastRowTarget = wks.Cells(Rows.Count, "A").End(xlUp).row 
    col = 1 
    For j = i To i + 3 
     For k = 2 To lastRowSource 
      wks.Cells(lastRowTarget + k - 1, col) = wks.Cells(k, j) 
      DoEvents 
     Next k 
     col = col + 1 
    Next j 
Next i 
Application.ScreenUpdating = True 
End Sub