2017-01-12 62 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

啊!對不起,我忘記了陣列正在尋找特定的數據。讓我更新並重試。 –

相關問題