Code not working when running but does when debugging?

时间:2017-07-11 13:47:13

标签: excel vba conditional-formatting

I've looked at many other posts about this similar issue, but they did not solve my problem.

I have data that contains bad values. I've used conditional formatting to mark these values red. Due to the size of the data it is not an option to have my script check the actual formula/values, so I have it check the displayed color. My script is supposed to loop through columns, looking for this color, when color is found it copies the row to another sheet (so it can be returned with another script later) and then deletes the original row and moves up to the next row. For the sake of size and speed I have limited the search area to a specific point where I know the data is bad, thus marked red (Row 337;Col 22.)

When I call the procedure by pressing a button, it does not see this red mark. When I step through the code, it does. I managed to pin the problem down on the part where I loop through the columns, but I cannot figure out what I'm doing wrong.

My Code:

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 when running:

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 when debugging:

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

1 个答案:

答案 0 :(得分:0)

好的,我发现原因 为什么 没有找到红色标记。识别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

我不确定为什么会这样。我想知道为什么会有这种差异,以及如何避免/处理它。我想我可以添加一个OR语句来让它同时查找-5和3,这样它在运行和调试时都能正常工作。 我应该问这个新问题还是继续?