2017-05-22 137 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

我明白你的观点,但这就是为什么我有这样的代码:如果计数器

相关问题