Excel - 宏可以通过更改电子表格动态地重新排序列,行

时间:2017-01-12 15:41:10

标签: excel vba excel-vba

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

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

基本上需要发生在电子表格中的是需要删除的前10行,你需要保留第11行和第12行,然后从那里,你可以看到有第12行中的名字“外套和Tails“,Bryce 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 个答案:

答案 0 :(得分: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