2017-07-27 69 views
0

我有這段代碼正在查看一列(在XFD1的另一頁上)並從該列中的值創建一個數組。然後它在當前工作表的一行中搜索這些值。當它找到匹配項時,它將剪切該列並將其插入到與數組中的值的順序相對應的位置。根據數組中的值的順序排列列 - 並且按鈕消失

我沒有收到任何編譯錯誤。我在工作表上放置了一個按鈕(不是ActiveX),並用它來執行代碼。這裏是我看到的:

  1. 什麼都沒有發生。列根本不移動。
  2. 計算機顯然是在「思考」,因爲旋轉的演出正在旋轉。
  3. 這裏是神祕的部分 - 按鈕消失!它永遠不會回來。我在工作表上放了幾個按鈕,並試用了所有按鈕。該按鈕每次都會消失。

我真的需要得到這個工作。我所需要的是將列重新排列到與另一張表上的列表相同的順序(列表中的95個項目)。我認爲這段代碼可以做到這一點,但我似乎已經進入了Twilight Zone,事情並不像他們看起來那樣(至少從我的角度來看)!

這就是:

Sub Reorder_Columns() 
    Dim arrColumnOrder(1 To 95) As String 
    Dim index As Integer 
    Dim Found As range 
    Dim tick As Integer 

     For index = 1 To 95 
      arrColumnOrder(index) = UserFormDropDownDataSheet.range("XFD1") 
     Next index 

    Application.ScreenUpdating = False 
    tick = 1 
     For index = LBound(arrColumnOrder) To UBound(arrColumnOrder) 
      Set Found = Rows("1:1").Find(arrColumnOrder(index), LookIn:=xlValues, LookAt:=xlWhole, _ 
      SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 
       If Not Found Is Nothing Then 
        If Found.column <> tick Then 
        Found.EntireColumn.Cut 
        Columns(tick).Insert Shift:=xlToRight 
        Application.CutCopyMode = False 
        End If 
       tick = tick + 1 
       End If 
     Next index 

    Application.ScreenUpdating = True 
End Sub 
+2

如果將95個相同值的實例放入數組中,您會如何重新排序? – Jeeped

+0

由於列插入,按鈕可能已「移動」。 –

+0

您的按鈕消失,因爲選中「剪切,複製和粘貼插入的對象及其父級單元」選項未被選中。 *選項>>高級>>剪切,複製和粘貼* –

回答

0

回答關於什麼是錯我的原代碼的問題是:

首先,我試圖設置數組的大小,但應該是使用動態數組,因爲我期望我的數組列中的數據將增加,因爲我向要排序的工作表添加更多列。所以,Dim arrColumnOrder(1 To 95) As String應該是Dim arrColumnOrder As Variant

我當時想我的數組遍歷與

For index = 1 To 95 
     arrColumnOrder(index) = UserFormDropDownDataSheet.range("XFD1") 
    Next index 

這當然是錯誤的。我換成這與

arrColumnOrder = UserFormDropDownDataSheet.range("XFD1:XFD95").Value 

然後,在

 Set Found = Rows("1:1").Find(arrColumnOrder(index), LookIn:=xlValues, LookAt:=xlWhole, _ 
     SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 

本來應該「... Find (arrColumnOrder(index, 1) ...」

答案爲何按鈕被感動的是,我沒有設置按鈕的格式選項(右鍵單擊按鈕,格式控制>屬性>選擇「不要移動或使用單元格大小」。)所以當事情正在移動(並且奇怪,因爲我的代碼都是錯誤的)該列被複制和粘貼時的單元格。

這是我最終的代碼,它的工作原理和完成它所期望的。也就是說,它會根據「XFD1:XFD95」範圍內的數據(在單獨的工作表上,我按照正確的順序存儲列標題)創建一個數組,然後對活動工作表中的列進行排序以匹配陣列。我不想顯式調用工作表名稱,因爲這將在不同的工作表上運行。使用Find而不是Match對我來說工作得很好,因爲這不是我處理的大量數據,所以速度不是問題。

Sub Reorder_Columns() 
    Dim arrColumnOrder As Variant 
    Dim index As Interger 
    Dim Found As range 
    Dim tick As Integer 

    arrColumnOrder = UserFormDropDownDataSheet.range("XFD1:XFD95").Value 
    Application.ScreenUpdating = False 
    tick = 1 
     For index = LBound(arrColumnOrder) To UBound(arrColumnOrder) 
      Set Found = Rows("1:1").Find(arrColumnOrder(index, 1), LookIn:=xlValues, LookAt:=xlWhole, _ 
      SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 
       If Not Found Is Nothing Then 
        If Found.column <> tick Then 
        Found.EntireColumn.Cut 
        Columns(tick).Insert Shift:=xlToRight 
        Application.CutCopyMode = False 
        End If 
       tick = tick + 1 
       End If 
     Next index 
    Application.ScreenUpdating = True 
End Sub 

對於我來說,最大的教訓之一是這裏不嘗試寫代碼時,我都只睡了兩個小時!我真的很累,犯了一些愚蠢的錯誤,因爲我沒有清楚地思考。我得到了一個很好的休息,然後今天早上我很容易地看到我出錯的地方。