代码在 运行 时不工作,但在调试时工作?
Code not working when running but does when debugging?
我看了很多关于这个类似问题的其他帖子,但他们没有解决我的问题。
我有包含错误值的数据。我使用条件格式将这些值标记为红色。由于数据的大小,我的脚本无法检查实际的 formula/values,所以我让它检查显示的颜色。
我的脚本应该遍历列,寻找这种颜色,当找到颜色时,它会将行复制到另一个 sheet (这样它可以稍后与另一个脚本一起返回)然后删除原始行并向上移动到下一行。
为了大小和速度,我将搜索区域限制在我知道数据有问题的特定点,因此标记为红色(第 337 行;第 22 列。)
当我按下按钮调用程序时,它没有看到这个红色标记。当我单步执行代码时,它确实如此。我设法将问题固定在循环遍历列的部分,但我无法弄清楚我做错了什么。
我的代码:
Dim intSerialCount As Integer
intSerialCount = Sheet4.Range("I1").Value
Dim intBadDataSerialNumberStart As Integer
intBadDataSerialNumberStart = 3
Dim intBadDataSerialNumberCount As Integer
intBadDataSerialNumberCount = Sheet6.Cells(1, 2).Value
Dim intRowCnt As Integer
Dim intBeginRow As Integer
intBeginRow = intSerialCount + intBadDataSerialNumberStart - 1
Dim intEndRow As Integer
intEndRow = 333 'intBadDataSerialNumberStart
Dim intColCnt As Integer
Dim intBeginCol As Integer
intBeginCol = 21 '7
Dim intEndCol As Integer
intEndCol = 23 '37
Dim button As MSForms.CommandButton
Set button = Sheets("ANALYSIS TOOL").CommandButton2
Dim strNoMatch As String
strNoMatch = "Something went wrong!"
Dim strTitle As String
strTitle = "KPI Tool"
Dim strPW As String
strPW = "******"
'========================================================================================
Application.ScreenUpdating = False
Debug.Print "checking sourcedata"
If Sheet2.Range("A1").Value = "" Then
Debug.Print "trimming sourcedata"
Sheet2.Activate
Sheet2.Columns("C:C").Select
Selection.Replace What:=" ", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Sheet2.Range("A1").Value = 1
Else
Debug.Print "Sourcedata already trimmed"
End If
If Sheet4.Range("H1").Value = False Then
Debug.Print "BadData already removed? = FALSE"
'----------------------------------------------------------------------------------------
'Reorganise_TruckAnalysis
Sheet3.AutoFilter.Sort.SortFields.Clear
Sheet3.AutoFilter.ShowAllData
Sheet3.Range("A3").FormulaR1C1 = "=IF(DATA!RC[2]=0,NA(),DATA!RC[2])" '=IF(DATA!C3=0;NA();DATA!C3)
Sheet3.Activate
Sheet3.Range("A3").Select
Sheet3.Range("A3").AutoFill Destination:=Range("A3:A500")
Sheet3.Calculate
Debug.Print "Reorganised TruckAnalysis"
'Loop Rows:
Debug.Print "Start looping rows"
For intRowCnt = intBeginRow To intEndRow Step -1
Debug.Print "Checking row " & intRowCnt
If IsError(Sheet3.Cells(intRowCnt, 1)) Then
Debug.Print "IsError found"
'do nothing, go to next row
Else
'Loop Columns:
Debug.Print "Start looping columns"
For intColCnt = intBeginCol To intEndCol
Debug.Print "Checking column " & intColCnt
If Sheet3.Cells(intRowCnt, intColCnt).DisplayFormat.Interior.ColorIndex = 3 Then
Debug.Print "Red Mark found"
If Sheet2.Cells(intRowCnt, 3).Value = Sheet3.Cells(intRowCnt, 1).Value Then
Debug.Print "Data Matches"
Application.CutCopyMode = False
Sheet2.Cells(intRowCnt, 3).EntireRow.Copy
Debug.Print "copying bad data"
Sheet6.Cells(intBadDataSerialNumberStart + intBadDataSerialNumberCount, 1).PasteSpecial Paste:=xlPasteFormats
Sheet6.Cells(intBadDataSerialNumberStart + intBadDataSerialNumberCount, 1).PasteSpecial Paste:=xlPasteValues
Sheet2.Cells(intRowCnt, 3).EntireRow.Delete Shift:=xlUp
Debug.Print "removing bad data from source"
intBadDataSerialNumberCount = intBadDataSerialNumberCount + 1
Exit For
Else
Debug.Print "Data doesn't match"
MsgBox strNoMatch, _
vbOKOnly + vbInformation, strTitle
End If
End If
Debug.Print "No Red mark found"
Next intColCnt
Debug.Print "Restarting column count"
intColCnt = intBeginCol
End If
Debug.Print "Finished looping columns"
Next intRowCnt
Debug.Print "Finished looping rows"
'Reorganise_TruckAnalysis
Debug.Print "Reorganising TruckAnalysis"
Sheets("TRUCK ANALYSIS").Unprotect Password:=strPW
Sheet3.AutoFilter.ShowAllData
Sheet3.Range("A3").FormulaR1C1 = "=IF(DATA!RC[2]=0,NA(),DATA!RC[2])" '=IF(DATA!C3=0;NA();DATA!C3)
Sheet3.Activate
Sheet3.Range("A3").Select
Sheet3.Range("A3").AutoFill Destination:=Range("A3:A500")
Sheet3.Calculate
Sheet4.Range("H1").Value = True
button.Caption = "RETURN BAD DATA"
Else
Debug.Print "BadData already removed? = TRUE"
'SCRIPT FOR RETURNING BAD DATA
If intBadDataSerialNumberCount > 0 Then
Debug.Print "Secured Bad Data found"
intBeginRow = intBadDataSerialNumberStart
intEndRow = intBadDataSerialNumberStart + intBadDataSerialNumberCount
Debug.Print "Start looping rows"
For intRowCnt = intBeginRow To intEndRow
Debug.Print "checking row " & intRowCnt
Application.CutCopyMode = False
Sheet6.Cells(intRowCnt, 1).EntireRow.Copy
Sheet2.Cells(intSerialCount + intBadDataSerialNumberStart, 1).PasteSpecial Paste:=xlPasteValues
Sheet6.Cells(intRowCnt, 1).EntireRow.Clear
Next intRowCnt
End If
Sheets("ANALYSIS TOOL").Unprotect Password:=strPW
Sheet4.Range("H1").Value = False
button.Caption = "REMOVE BAD DATA"
'----------------------------------------------------------------------------------------
End If
Sheet4.Activate
End Sub
debug.print 当 运行:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
No Red mark found
Checking column 22
No Red mark found
Checking column 23
No Red mark found
Restarting column count
Finished looping columns
Debug.print调试时:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
No Red mark found
Checking column 22
Red Mark found
Data Matches
copying bad data
removing bad data from source
Restarting column count
Finished looping columns
好的,我找到了为什么找不到红色标记的原因。 displayformat.colorindex 号码的识别似乎有所不同。我添加了 debug.print 请求找到的 colorindex 和 displayformat.colorindex 这就是我得到的:
Debug.print running:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Checking column 22
DisplayFormat.Interior.ColorIndex = -5
Interior.ColorIndex = 24
No Red mark found
Checking column 23
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Restarting column count
Finished looping columns
Debug.print debugging:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Checking column 22
DisplayFormat.Interior.ColorIndex = 3
Interior.ColorIndex = 24
Red Mark found
Data Matches
copying bad data
removing bad data from source
Restarting column count
Finished looping columns
我不确定为什么会这样。我想知道为什么会有这种差异,以及如何 avoid/handle 它。我想我可以添加一个 OR 语句让它同时查找 -5 和 3,这样它就可以在 运行 和调试时工作。
我应该就此提出新问题还是继续此处?
我看了很多关于这个类似问题的其他帖子,但他们没有解决我的问题。
我有包含错误值的数据。我使用条件格式将这些值标记为红色。由于数据的大小,我的脚本无法检查实际的 formula/values,所以我让它检查显示的颜色。 我的脚本应该遍历列,寻找这种颜色,当找到颜色时,它会将行复制到另一个 sheet (这样它可以稍后与另一个脚本一起返回)然后删除原始行并向上移动到下一行。 为了大小和速度,我将搜索区域限制在我知道数据有问题的特定点,因此标记为红色(第 337 行;第 22 列。)
当我按下按钮调用程序时,它没有看到这个红色标记。当我单步执行代码时,它确实如此。我设法将问题固定在循环遍历列的部分,但我无法弄清楚我做错了什么。
我的代码:
Dim intSerialCount As Integer
intSerialCount = Sheet4.Range("I1").Value
Dim intBadDataSerialNumberStart As Integer
intBadDataSerialNumberStart = 3
Dim intBadDataSerialNumberCount As Integer
intBadDataSerialNumberCount = Sheet6.Cells(1, 2).Value
Dim intRowCnt As Integer
Dim intBeginRow As Integer
intBeginRow = intSerialCount + intBadDataSerialNumberStart - 1
Dim intEndRow As Integer
intEndRow = 333 'intBadDataSerialNumberStart
Dim intColCnt As Integer
Dim intBeginCol As Integer
intBeginCol = 21 '7
Dim intEndCol As Integer
intEndCol = 23 '37
Dim button As MSForms.CommandButton
Set button = Sheets("ANALYSIS TOOL").CommandButton2
Dim strNoMatch As String
strNoMatch = "Something went wrong!"
Dim strTitle As String
strTitle = "KPI Tool"
Dim strPW As String
strPW = "******"
'========================================================================================
Application.ScreenUpdating = False
Debug.Print "checking sourcedata"
If Sheet2.Range("A1").Value = "" Then
Debug.Print "trimming sourcedata"
Sheet2.Activate
Sheet2.Columns("C:C").Select
Selection.Replace What:=" ", _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Sheet2.Range("A1").Value = 1
Else
Debug.Print "Sourcedata already trimmed"
End If
If Sheet4.Range("H1").Value = False Then
Debug.Print "BadData already removed? = FALSE"
'----------------------------------------------------------------------------------------
'Reorganise_TruckAnalysis
Sheet3.AutoFilter.Sort.SortFields.Clear
Sheet3.AutoFilter.ShowAllData
Sheet3.Range("A3").FormulaR1C1 = "=IF(DATA!RC[2]=0,NA(),DATA!RC[2])" '=IF(DATA!C3=0;NA();DATA!C3)
Sheet3.Activate
Sheet3.Range("A3").Select
Sheet3.Range("A3").AutoFill Destination:=Range("A3:A500")
Sheet3.Calculate
Debug.Print "Reorganised TruckAnalysis"
'Loop Rows:
Debug.Print "Start looping rows"
For intRowCnt = intBeginRow To intEndRow Step -1
Debug.Print "Checking row " & intRowCnt
If IsError(Sheet3.Cells(intRowCnt, 1)) Then
Debug.Print "IsError found"
'do nothing, go to next row
Else
'Loop Columns:
Debug.Print "Start looping columns"
For intColCnt = intBeginCol To intEndCol
Debug.Print "Checking column " & intColCnt
If Sheet3.Cells(intRowCnt, intColCnt).DisplayFormat.Interior.ColorIndex = 3 Then
Debug.Print "Red Mark found"
If Sheet2.Cells(intRowCnt, 3).Value = Sheet3.Cells(intRowCnt, 1).Value Then
Debug.Print "Data Matches"
Application.CutCopyMode = False
Sheet2.Cells(intRowCnt, 3).EntireRow.Copy
Debug.Print "copying bad data"
Sheet6.Cells(intBadDataSerialNumberStart + intBadDataSerialNumberCount, 1).PasteSpecial Paste:=xlPasteFormats
Sheet6.Cells(intBadDataSerialNumberStart + intBadDataSerialNumberCount, 1).PasteSpecial Paste:=xlPasteValues
Sheet2.Cells(intRowCnt, 3).EntireRow.Delete Shift:=xlUp
Debug.Print "removing bad data from source"
intBadDataSerialNumberCount = intBadDataSerialNumberCount + 1
Exit For
Else
Debug.Print "Data doesn't match"
MsgBox strNoMatch, _
vbOKOnly + vbInformation, strTitle
End If
End If
Debug.Print "No Red mark found"
Next intColCnt
Debug.Print "Restarting column count"
intColCnt = intBeginCol
End If
Debug.Print "Finished looping columns"
Next intRowCnt
Debug.Print "Finished looping rows"
'Reorganise_TruckAnalysis
Debug.Print "Reorganising TruckAnalysis"
Sheets("TRUCK ANALYSIS").Unprotect Password:=strPW
Sheet3.AutoFilter.ShowAllData
Sheet3.Range("A3").FormulaR1C1 = "=IF(DATA!RC[2]=0,NA(),DATA!RC[2])" '=IF(DATA!C3=0;NA();DATA!C3)
Sheet3.Activate
Sheet3.Range("A3").Select
Sheet3.Range("A3").AutoFill Destination:=Range("A3:A500")
Sheet3.Calculate
Sheet4.Range("H1").Value = True
button.Caption = "RETURN BAD DATA"
Else
Debug.Print "BadData already removed? = TRUE"
'SCRIPT FOR RETURNING BAD DATA
If intBadDataSerialNumberCount > 0 Then
Debug.Print "Secured Bad Data found"
intBeginRow = intBadDataSerialNumberStart
intEndRow = intBadDataSerialNumberStart + intBadDataSerialNumberCount
Debug.Print "Start looping rows"
For intRowCnt = intBeginRow To intEndRow
Debug.Print "checking row " & intRowCnt
Application.CutCopyMode = False
Sheet6.Cells(intRowCnt, 1).EntireRow.Copy
Sheet2.Cells(intSerialCount + intBadDataSerialNumberStart, 1).PasteSpecial Paste:=xlPasteValues
Sheet6.Cells(intRowCnt, 1).EntireRow.Clear
Next intRowCnt
End If
Sheets("ANALYSIS TOOL").Unprotect Password:=strPW
Sheet4.Range("H1").Value = False
button.Caption = "REMOVE BAD DATA"
'----------------------------------------------------------------------------------------
End If
Sheet4.Activate
End Sub
debug.print 当 运行:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
No Red mark found
Checking column 22
No Red mark found
Checking column 23
No Red mark found
Restarting column count
Finished looping columns
Debug.print调试时:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
No Red mark found
Checking column 22
Red Mark found
Data Matches
copying bad data
removing bad data from source
Restarting column count
Finished looping columns
好的,我找到了为什么找不到红色标记的原因。 displayformat.colorindex 号码的识别似乎有所不同。我添加了 debug.print 请求找到的 colorindex 和 displayformat.colorindex 这就是我得到的:
Debug.print running:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Checking column 22
DisplayFormat.Interior.ColorIndex = -5
Interior.ColorIndex = 24
No Red mark found
Checking column 23
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Restarting column count
Finished looping columns
Debug.print debugging:
checking sourcedata
Sourcedata already trimmed
BadData already removed? = FALSE
Reorganised TruckAnalysis
Start looping rows
Checking row 337
Start looping columns
Checking column 21
DisplayFormat.Interior.ColorIndex = 24
Interior.ColorIndex = 24
No Red mark found
Checking column 22
DisplayFormat.Interior.ColorIndex = 3
Interior.ColorIndex = 24
Red Mark found
Data Matches
copying bad data
removing bad data from source
Restarting column count
Finished looping columns
我不确定为什么会这样。我想知道为什么会有这种差异,以及如何 avoid/handle 它。我想我可以添加一个 OR 语句让它同时查找 -5 和 3,这样它就可以在 运行 和调试时工作。 我应该就此提出新问题还是继续此处?