2016-02-12 75 views
2

我想運行一個代碼,我也在這裏找到。該代碼會刪除工作簿上每個拼板表上每個列上的重複項,將其作爲單獨的實體處理。每當我嘗試運行代碼時,編譯器錯誤都會提示「sub或function not defined」,最上面的部分有黃色突出顯示,「LastCell」顯示藍色突出顯示。我已經添加了求解器參考,但它仍然給我同樣的錯誤。我只是無法弄清楚是什麼問題,如果它在代碼上,或者我應該添加另一個引用。excel vba中的子​​函數或函數沒有定義的錯誤

Sub Removeduplicates() 

    Dim ws As Workbook 
    Dim lLastcol As Long 
    Dim lLastrow As Long 
    Dim i As Long 


    For Each ws In ThisWorkbook.Worksheets 
     lLastcol = LastCell(ws).Column 

     For i = 1 To lLastcol 

      lLastrow = LastCell(ws, i).Row 

      With ws 
       .Range(.Cells(1, i), .Cells(lLastrow, i)).Removeduplicates Columns:=1, Header:=xlNo 
      End With 


     Next i 

    Next ws 

End Sub 
+0

請將問題中的代碼作爲文本發佈,而不是圖像。 –

+0

請參閱[爲什麼不顯示代碼和示例數據的圖像](http://tinyurl.com/kdxb7le)。 – Jeeped

回答

2

看起來像拉絲細胞是你認爲你有的功能。我們是通過工作表。禰功能將使用類似

Function lastcell(w as worksheet) as range 
    Set Lastcell=w.range("a" & w.rows.count).end(xlup) 

End function 
+0

由於何時是[Range.Address屬性](https://msdn.microsoft.com/en-us/library/office/ff837625.aspx)[Range對象](https://msdn.microsoft.com/ EN-US /圖書館/辦公室/ ff838238.aspx)? – Jeeped

+0

對不起,我正在上班的路上在平板電腦上打字。 –

+0

你仍然需要設置一個[Range對象](https://msdn.microsoft.com/en-us/library/office/ff838238.aspx),如果你看看'LastCell'函數的兩個發生在代碼中,其中一個使用可選列參數,並且它們都不要求列A中的最後一個單元格,除非列A是工作表上唯一填充的列。此響應似乎在[this](http://i.stack.imgur.com/vAUaw.png)組的中間。 – Jeeped

0

如果你指的是達倫Bartrup庫克here的解決方案,確保功能LastCell複製到您的代碼。

1

解密你的代碼片段後,這是我能想到的最好的。

Function lastCell(ws As Worksheet, _ 
        Optional c As Variant, _ 
        Optional r As Variant) As Range 
    With ws 
     If IsMissing(c) And IsMissing(r) Then 
      Set lastCell = .Cells.SpecialCells(xlCellTypeLastCell) 
     ElseIf IsMissing(c) And Not IsMissing(r) Then 
      Set lastCell = .Cells(r, .Columns.Count).End(xlToLeft) 
     ElseIf IsMissing(r) And Not IsMissing(c) Then 
      Set lastCell = .Cells(.Rows.Count, c).End(xlUp) 
     Else 
      Set lastCell = .Cells(r, c) 
     End If 
    End With 
End Function 

將該代碼複製到VBA項目中的模塊代碼表。它可以用以下簡短的子程序進行測試。

Sub test() 
    Dim ws1 As Worksheet 

    Set ws1 = ActiveSheet 

    Debug.Print lastCell(ws1).Address(0, 0)  '<~~ last cell on worksheet 
    Debug.Print lastCell(ws1, 3).Address(0, 0) '<~~ last used cell in column C 
    Debug.Print lastCell(ws1, , 4).Address(0, 0) '<~~ last used column on row 4 
End Sub 
+0

你的意思是我將代碼複製到另一個模塊?感謝您的幫助。 – goody24

+0

不,您可以將其粘貼到已擁有的Removeduplicates子項下。它不能進入​​工作表代碼表(例如Sheet1);它必須是模塊代碼表(例如Module1)。 – Jeeped

+0

希望你不要介意,如果我問爲什麼有類型不匹配的運行時錯誤,當我嘗試運行代碼時,它突出顯示了For Each ws ThisWorkbook.Worksheets – goody24