Excel – macros可通过更改电子表格dynamic重新排列列,行

我的问题是如何使用VBmacros重新排列电子表格。 CSV电子表格是我们的客户的订单,从他们的运输软件中导出,需要上传到运输软件中执行。

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

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

我正在通过这些只是倒数多less,并删除行堆栈这些特定的行,但你可以看到多余的编码是。

有没有更好的解决scheme,可以根据每月有多less订单来dynamic更改?

我使用的代码是:

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 

正如你所看到的,这是非常多余的。 有反正清理呢?

尝试这个。

 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