2016-05-17 25 views
-1
Sub foo() 
    Dim lastUsedRowDiff As Long 
    Dim lastUsedColumnDiff As Long 
    Dim myWrkbook As Workbook 
    Dim mySheet As Worksheet 
    Dim columnArray() As Integer 
    Dim p As Integer 

    lastUsedRowDiff = ActiveSheet.UsedRange.Columns.Count 
    lastUsedColumnDiff = ActiveSheet.UsedRange.Columns.Count 
    ReDim columnArray(1 To lastUsedColumnDiff) 
    For p = 1 To lastUsedColumnDiff 
     p = columnArray(p) 
    Next 
    ActiveSheet.Range(Cells(1, 1), Cells(lastUsedRowDiff,    lastUsedColumnDiff)).RemoveDuplicates _ 
    Columns:=Array(columnArray) 

End Sub 
+0

嘗試使用調試器來查找它凍結的位置(或無限循環)。 F8可讓您逐行執行代碼。 – arcadeprecinct

回答

0

我可能寫的不僅僅是支持建議,但它的目的是作爲關於此主題的教育。請閱讀正確的結尾,因爲有重要的信息'刪除重複'。

我已經評論了整個代碼,以解釋我所看到的情況。核心問題'爲什麼會凍結'的答案是因爲你有一個無限循環。

Sub foo() 
    Dim lastUsedRowDiff As Long 
    Dim lastUsedColumnDiff As Long 
    Dim myWrkbook As Workbook 
    Dim mySheet As Worksheet 
    Dim columnArray() As Integer 
    Dim p As Integer 

    'If you are finding the last used row and col you should also find the 
    'first used row as it may not always be 1 
    'Dim LngFirstUsedCol  As Long 
    'Dim LngFirstUsedRow  As Long 
    'LngFirstUsedCol = ActiveSheet.UsedRange.Column 
    'LngFirstUsedRow = ActiveSheet.UsedRange.Row 

    'If this is for the row, it should take .Rows.count, not 'Columns.Count 
    'lastUsedRowDiff = ActiveSheet.UsedRange.Rows.Count 
    lastUsedRowDiff = ActiveSheet.UsedRange.Columns.Count 
    lastUsedColumnDiff = ActiveSheet.UsedRange.Columns.Count 

    'Also, the title of your varible refers to the difference but later they 
    'are used as the actual address 
    'i.e. I have a range that has 1 column in it, but it is on column E (5), 
    'your code would return 1 which is right as I only have 1 column, but 
    'when using that as an address if refers to column A (1) which is not 
    'right We would need the address to be roughly: - 
    ''First Used Column' + 'Used Column Count' 

    'I am assuming this is trying to build an array of all the columns? 
    'But is saying:- 
    ' From P (which starts at 1) to the number of the last used column 
    '  set P to 0 
    ' Move to the next P (i.e. Increment it to back up to 1) 
    'This is why you are stuck in a loop, turning them around would get past 
    'the issue to an extent 
    ReDim columnArray(1 To lastUsedColumnDiff) 
    For p = 1 To lastUsedColumnDiff 
     p = columnArray(p) 
     'columnArray(p) = p 
    Next 

    'The above code looks to include every column, by simply omitting the 
    ''Columns' parameter from the 'RemoveDuplicates' function, it would by 
    'default select all columns, so you could remove the For loop too 
    'You did not need to place 'Array' around your array as it is already 
    'an array 
    ActiveSheet.Range(Cells(1, 1), Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates Columns:=Array(columnArray) 
    'ActiveSheet.Range(Cells(1, 1), Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates 

    'In addition to the above, you should used the first row and column as 
    'well and not assume it is A1 (Cells(1,1)) 
    'ActiveSheet.Range(Cells(LngFirstUsedRow, LngFirstUsedCol), Cells(lastUsedRowDiff, lastUsedColumnDiff)).RemoveDuplicates 

    'Finally as mentioned prior, you are using the difference as the address 
    'in error 
    'ActiveSheet.Range(Cells(LngFirstUsedRow, LngFirstUsedCol), Cells(LngFirstUsedRow + (lastUsedRowDiff - 1), LngFirstUsedCol + (lastUsedColumnDiff))).RemoveDuplicates 

    'Orrrr this whole proc can be reduced to a single line of three words 
    ActiveSheet.UsedRange.RemoveDuplicates 

End Sub 

最後,這是非常重要的,讀什麼Richard Michaels不得不說的在this thread RemoveDuplicates。基本上,在VBA中它不是一個安全的功能,我會建議您手動編寫代碼來刪除重複項,或確保它保持在單列範圍的安全參數範圍內,並且不通過變量Columns

+0

它不起作用,它再次凍結。我只是要使用先進的過濾器或其他東西 – mitzy