2017-01-12 42 views
0

我的问题是如何使用VB宏重新排列电子表格。 CSV电子表格是我们的客户的订单,它是从他们的运输软件中导出的,需要上传到运输软件中执行。Excel - 宏可通过更改电子表格动态重新排列列,行

在我非常基本的VB理解中遇到的问题是我用来尝试写这个的极端冗余。我知道如何删除行,列等。我不知道该怎么做才能让它变成动态变化的电子表格(三月份的IE 20订单,四月份的40份订单)

基本上需要发生什么电子表格是前10行将需要删除,您将需要保持行11和12,然后从那里,你可以看到有名称,如行12“外套和尾巴”,布莱斯Dishongh,地址,等跨越行。这些是需要保存在电子表格中的唯一行,它们每14或15行取决于。

我正在经历这些只是倒数多少,并删除行堆叠这些特定的行,但你可以看到多余的编码是。

有没有更好的解决方案,可以根据每月有多少订单来动态更改?

我使用的代码是:

Sub Delete() 

Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 
Rows(1).EntireRow.Delete 

Columns(1).EntireColumn.Delete 

Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 
Rows(3).EntireRow.Delete 

Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 
Rows(4).EntireRow.Delete 

Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 
Rows(5).EntireRow.Delete 

Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 
Rows(6).EntireRow.Delete 

Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 
Rows(7).EntireRow.Delete 

Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 
Rows(8).EntireRow.Delete 

Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 
Rows(9).EntireRow.Delete 


End Sub 

正如你可以看到这是非常多余。无论如何要清理这个?

+1

如果您显示您当前尝试的代码并讨论一下,您将有更多的机会获得帮助。 –

回答

1

试试这个。

Sub mySub() 
    ' First, define an array for the words that you need to keep. 
    Dim words: words = Array("Coat and Tails", "Bryce Dishongh", "Address") ' <-- etc.. 

    With ActiveSheet '<-- I hate this, but to simplify your task 
     .Columns(1).Delete 
     For i = .UsedRange.Row + .UsedRange.Rows.Count - 1 To 13 Step -1 
      If Application.Sum(Application.CountIf(.Rows(i), words)) = 0 Then .Rows(i).Delete 
     Next 

     .Rows("1:10").Delete 
    End With 
End Sub 
+0

谢谢,这非常接近。问题在于它只触及需要的第一行信息,电子表格的其余部分随之消失。 –

+0

您是否有电子邮件我可以提供CSV文件给您,以便您自己看看? –

+0

啊!对不起,我忘记了阵列正在寻找特定的数据。让我更新并重试。 –

相关问题