2017-06-21 59 views
0

所以我是vba的新手,我試圖讓宏來比較單元格,並在它旁邊的列中輸出一個計數器。這裏是我的代碼:Excel VBA很多問題

Sub Duplicate_Count() 

'Find the last used row in a Column: column A in this example 

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Integer 
counter = 1 

With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow 

    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    Sheet1.Cells(i, "N") = counter 

Next i 

末次

好了,所以這段代碼運行,它看起來它的工作原理,列「N」開始填充,但程序凍結了,我不知道如果僅僅是因爲文件太大以致需要很多時間,或者如果出現錯誤。如果我重新啓動程序,我得到運行時錯誤'-2147417848(80010108)':對象「範圍」的方法'_Default'失敗。只是讓愚蠢的錯誤

編輯: 子Duplicate_Count() 「查找列中的最後一次使用的行:A列在這個例子中

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Long 
counter = 0 
Dim sht As Worksheet 
Set sht = Worksheets("Sheet1") 

With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow 

    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    sht.Cells(i, "N") = counter 

Next i 

末次

此代碼崩潰每次,和偶爾會給我一個錯誤:運行時錯誤'-2147417848(80010108)':對象「Range」的方法'_Default'失敗。我不知道如何解決這個問題......甚至是什麼意思。

+0

我注意到的第一個潛在問題是您每次循環時都將計數器設置爲1,您是否希望這樣做?同時,每當你循環時,你都不需要'調暗'每個變量。 – Dexloft

+0

不,我意識到可以將其移出循環,謝謝! – Josh

+0

也許你可以使用簡單= COUNTIF($ A $ 1:$ A $ 1000,A1)? – jkpieterse

回答

0

Alrighty這是我完成的代碼:

子Duplicate_Count()

Dim LastRow As Long 
Dim value1 As String 
Dim value2 As String 
Dim counter As Long 
counter = 0 
Dim sht As Worksheet 
Set sht = Worksheets("Sheet1") 

'Find the last used row in Column L 
With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row 
End With 

'Search down row for duplicates 
Dim i As Long 

For i = 1 To LastRow - 1 


    'Sets value1 and value2 to be compared 
    value1 = Worksheets("Sheet1").Cells(i, "L").Value 
    value2 = Worksheets("Sheet1").Cells(i + 1, "L").Value 

    'If values are not diferent then counter will not increment 
    If value1 <> value2 Then 
     counter = counter + 1 
    End If 

    'Sets the n colom to count, duplicates should not increment the counter 
    sht.Cells(i + 1, "N") = counter 

Next i 

末次

謝謝你這麼多大家的幫助!原來,這些值是字符串,因爲我看了一些標題,設置工作表是我最大的問題之一,至於它有運行時錯誤,我相信這只是因爲文檔太長了。我讓它坐了30分鐘,它完成得很好。 再次感謝大家的幫助!

0

我在運行代碼時沒有遇到錯誤,但我做了一些更改,我認爲可能會修復它。試試這個,讓我知道會發生什麼!

Sub TommysMacro() 
    'Find the last used row in a Column: column A in this example 
    Dim LastRow As Long 
    Dim counter As Integer 

    LastRow = Sheets("Sheet1").Cells(65536, "L").End(xlUp).Row + 1 

    'Sets values to be compared 
    ReDim cellValue(1 To LastRow) As String 
    For i = 1 To LastRow 
     cellValue(i) = Worksheets("Sheet1").Cells(i, "L").Value 
    Next i 

    'Search down row for duplicates 
    For i = 1 To LastRow - 1 

     'If values are not diferent then counter will not increment 
     If cellValue(i) <> cellValue(i + 1) Then 
      counter = counter + 1 
     End If 

     'Sets the n column to count, duplicates should not increment the counter 
     Sheets("Sheet1").Cells(i, "N").Value = counter 

    Next i 

End Sub 

我剛剛改變它後,我看到你的評論關於它是一個大列,我認爲這應該快得多!

+0

是'Sheet1'沒有被定義,並給出了'Object Required'錯誤。我也會設置一個變量作爲工作表。 'Dim sht as Worksheet'和'Set sht = Worksheets(「Sheet1」)'。現在,您可以使用'sht'而不是'Worksheets(「Sheet1」)',這使得代碼更加方便。爲了縮短這個時間,你可以在Dim'Mehtods之後爲整個代碼使用'With sht'。 – UGP

+0

什麼是L65536? – Josh

+0

在Excel 2003和這樣的舊版本中,它是可能的最大行值。既然你得到了一個錯誤,我沒有(我正在使用Excel 2007),我想你可能有一箇舊版本,所以我只是用它來防萬一。基本上,它進入L列的最後一行,然後從那裏使用'.End(xlUp)'獲得L中最後一個使用的行。但是我注意到我的方法有錯誤,它不會執行最後一行在列中,因爲我的循環去LastRow - 1 – Dexloft