2017-05-22 70 views
0

Im在運行代碼時未在調試模式下刪除行。我把星星放在線旁邊給我一個問題。在調試模式下工作,但通常不會運行代碼。任何幫助?我曾嘗試使用doevent,但在for循環的開始,但沒有奏效。VBA代碼只在調試模式下運行時刪除行

Public Sub ItemUpdate(ByVal startRow As Integer, ByVal endRow As Integer, ByVal itemCol As String, ByVal statusCol As String, ByVal manuPNCol As String) 
Dim orgSheet As Worksheet 
Dim commonSheet As Worksheet 
Dim partDesCol As String 
Dim partDes As String 
Dim vendorColNumber As Integer 
Dim vendorColLetter As String 
Dim manuPN As String 
Dim counter As Integer 
Dim replaceRnge As Range 
Set orgSheet = ThisWorkbook.ActiveSheet 

partDesCol = FindPartDesCol() 

Set commonSheet = ThisWorkbook.Worksheets("Common Equipment") 

For counter = startRow To endRow 
'Get part description value 
partDes = Range(partDesCol & counter).Value 
'Delete row of empty cells if there is any 

If partDes = "" Then 
'deleteing empty row 
orgSheet.Rows(counter).Delete '************************** Only works in      
debug mode. 
endRow = endRow - 1 
If counter < endRow Then 
    counter = counter - 1 
Else 
    Exit For 
End If 

Else 

manuPN = Range(manuPNCol & counter).Value 
'Search for user part in common sheet 
Set rangeFind = commonSheet.Range("1:200").Find(partDes, lookat:=xlWhole) 
If rangeFind Is Nothing Or partDes = "" Then 
Debug.Print "Part " & partDes & " not found in Common Equipment" 
'MsgBox "Part " & partDes & " not found in Common Equipment" 
'Now check if manuPN is in common equipment 
Set rangeFind = commonSheet.Range("1:200").Find(manuPN, lookat:=xlWhole) 
    If rangeFind Is Nothing Or partDes = "" Then 
    Debug.Print "PartNumber " & manuPN & " not found in Common Equipment" 
    'Now check if vendor value of item is empty 
    'Get vendor col 
    vendorCol = FindSearchCol() 
    If orgSheet.Range(vendorCol & counter).Value = "" Then 
    'Copy and paste manufact. data to vendor 
    'converting from letter column to number and visa versa 
    vendorColNumber = Range(vendorCol & 1).Column 
    ManuColTemp = vendorColNumber - 2 
    ManuPNColTemp = vendorColNumber - 1 
    VendorPNColTemp = vendorColNumber + 1 
    ManuCol = Split(Cells(1, ManuColTemp).Address(True, False), "$")(0) 
    manuPNCol = Split(Cells(1, ManuPNColTemp).Address(True, False), "$")(0) 
    VendorPNCol = Split(Cells(1, VendorPNColTemp).Address(True, False), "$")  
(0) 
    orgSheet.Range(ManuCol & counter & ":" & manuPNCol & counter).Copy  Range(vendorCol & counter & ":" & VendorPNCol & counter) 

End If 
Else 
'Copy new data from common equipment and paste in place of old data 
'Get value of status 
If statusCol <> "error" Then 
    orderStatus = orgSheet.Range(statusCol & counter).Value 
End If 

commonSheet.Rows(rangeFind.Row).EntireRow.Copy 
orgSheet.Range(itemCol & counter).PasteSpecial xlPasteValues 

If statusCol <> "error" Then 
    orgSheet.Range(statusCol & counter).Value = orderStatus 
End If 

End If 

Else 
'Copy new data from common equipment and paste in place of old data 
'Get value of status 
If statusCol <> "error" Then 
    orderStatus = orgSheet.Range(statusCol & counter).Value 
End If 

commonSheet.Rows(rangeFind.Row).EntireRow.Copy 
orgSheet.Range(itemCol & counter).PasteSpecial xlPasteValues 

If statusCol <> "error" Then 
    orgSheet.Range(statusCol & counter).Value = orderStatus 
End If 

End If 
End If 
Next counter 

'call renumber item numbers 
Call NumberItems(0, 0, 0, False) 

End Sub 
+0

「僅適用於調試」通常與未完全定義所有範圍引用有關。首先確保對範圍/單元格的每次調用都有合格的工作表參考。 –

+0

您是否檢查過您的FindPartDesCol函數返回的內容? – YowE3K

+0

蒂姆威廉姆斯,我經歷了並修復了我所有的參考文獻,但它仍然無效。 –

回答

0

最有可能的是,你需要在你的範圍後退。當你邁出第一步,因爲你正在做的,計數器將跳過一排,每當你刪除行:

For counter = startRow To endRow 

更改爲

For counter = endRow To startRow Step -1 

此外,你應該申報endRowstartRow數據類型LongInteger的範圍不包括Excel工作表中的所有行;而且據說VBA在進行數學運算時將Integers轉換爲Longs。

+0

我明白你的觀點,但這就是爲什麼我有這樣的代碼:如果計數器

相關問題