简体   繁体   中英

Excel VBA code works in Debug mode, or after data has been re-input

This code previously worked, I changed some of the strings and it has started malfunctioning. About halfway through the script it will start assigning "N/A" for about 10 lines then proceeds to assign numbers again. Example, "O" + i returns "level 1" and is changed to "N/A" with the other affected column changing as well as if it registered a "level 0", it will then place the "1" further down, after about 20 instances of offset placement the code returns to working. If I re-input just column O and run it again, the code functions properly. I am greatful for any assistance.

PS, I am not a coder so I know it is kind of messy.

Sub Fix_Haz_Rec()


Dim lastRow3 As Integer 'last row of sheet 3
Sheets(3).Activate
lastRow3 = Cells(Rows.Count, 1).End(xlUp).Row

For i = 4 To lastRow3

ActiveSheet.Range("D" & i).Value = Math.Round(ActiveSheet.Range("D" & i).Value, 3)
ActiveSheet.Range("E" & i).Value = Math.Round(ActiveSheet.Range("E" & i).Value, 3)
ActiveSheet.Range("F" & i).Value = Math.Round(ActiveSheet.Range("F" & i).Value, 3)
ActiveSheet.Range("G" & i).Value = Math.Round(ActiveSheet.Range("G" & i).Value, 3)
ActiveSheet.Range("I" & i).Value = Math.Round(ActiveSheet.Range("I" & i).Value, 2)
ActiveSheet.Range("J" & i).Value = Math.Round(ActiveSheet.Range("J" & i).Value, 3)
ActiveSheet.Range("K" & i).Value = Math.Round(ActiveSheet.Range("K" & i).Value, 3)
ActiveSheet.Range("L" & i).Value = Math.Round(ActiveSheet.Range("L" & i).Value, 3)
If ActiveSheet.Range("M" & i).Value = "N/A" Or ActiveSheet.Range("M" & i).Value = "#N/A" Then
Else

   If ActiveSheet.Range("O" & i) = "Level 4" Then
        ActiveSheet.Range("O" & i) = 4
    Else
        If ActiveSheet.Range("O" & i) = "Level 3" Then
            ActiveSheet.Range("O" & i) = 3
        Else
            If ActiveSheet.Range("O" & i) = "Level 2" Then
                ActiveSheet.Range("O" & i) = 2
            Else
                If ActiveSheet.Range("O" & i) = "Level 1" Then
                    ActiveSheet.Range("O" & i) = 1
                Else
                    If ActiveSheet.Range("O" & i) = "Level 0" Then
                        ActiveSheet.Range("L" & i) = ActiveSheet.Range("N" & i).Value / 12
                        ActiveSheet.Range("O" & i) = "N/A"
                    Else
                        If ActiveSheet.Range("O" & i) = ">Max." Then
                            ActiveSheet.Range("O" & i) = "> Max"
                        End If
                    End If
                End If
            End If
        End If
    End If
Next i


Sheets("Recommended").Activate
ActiveSheet.Range("A4:P" & lastRow3).Select
Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlYes
ActiveSheet.Range("D4:M" & lastRow3).Select
Selection.NumberFormat = "0.00"

End Sub

give this a go - it should go a long way to helping you to achieve what you're after.

Sort out the value replacements first, then move to the mopping up - ie keep trying to find "Level 0" and do the L / N operation until you can't find that value any more.

    With ActiveSheet.Range("O4:O" & lastRow3)
        .Replace "Level 4", 4, xlWhole
        .Replace "Level 3", 3, xlWhole
        .Replace "Level 2", 2, xlWhole
        .Replace "Level 1", 1, xlWhole
        .Replace ">Max.", "> Max", xlWhole

        Set rngLevel0 = .Find(What:="Level 0", LookAt:=xlWhole)
        Do While Not rngLevel0 Is Nothing
            ActiveSheet.Range("L" & rngLevel0.Row) = ActiveSheet.Range("N" & rngLevel0.Row).Value / 12
            rngLevel0.Value = "N/A"

            Set rngLevel0 = .Find(What:="Level 0", LookAt:=xlWhole)
        Loop

    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM