Excel VBA error code during first run only

2019-07-23 11:08发布

I have tried several methods to eliminate my VBA error but it does not help. During the first run, there will be error code 9. Upon running again, the code works fine. I have another code with no error but does not execute the VBA code correctly the first run. Upon running the code again, it execute the VBA code correctly.

I have tried using F8 to debug the problem, i have tried looping.

Sub TXNFont()

Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim lastColumn As Integer
    Dim rng As Range
    Set rng = ActiveSheet.Cells

    Sheets("TXN Speed").Select

    lastColumn = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
    End With

    Sheets("TXN Speed").Select
    Cells.Select
       With ActiveSheet
        .Select
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
          For Lrow = Lastrow To Firstrow Step -1
            With .Cells(Lrow, lastColumn)
                If Not IsError(.Value) Then
                    If .Value > 3500 Then .EntireRow.Font.Color = RGB(255, 0, 0)
              End If
            End With
        Next Lrow
    End With
    With Application
             .Calculation = CalcMode
    End With

    Sheets("TXN Speed").Select
    With ActiveSheet
        .Select
        Range("A1:A5").EntireRow.Font.Color = RGB(0, 0, 0)
        End With

End Sub

Sub NetFont()

    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim lastColumn As Integer
    Dim rng As Range
    Set rng = ActiveSheet.Cells

    With Sheets("Network").Select

    Cells.Select
    With Selection.Font
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With

    lastColumn = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
    Cells.Select


    Cells.Select
     With ActiveSheet
        .Select
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

          For Lrow = Lastrow To Firstrow Step -1
            With .Cells(Lrow, lastColumn)
                If Not IsError(.Value) Then
                    If .Value < 10 Then .EntireRow.Font.Color = RGB(255, 0, 0)
                End If
            End With
        Next Lrow
    End With

    With Application
             .Calculation = CalcMode
    End With

    Sheets("Network").Select
    With ActiveSheet
        .Select
        Range("A1:A5").EntireRow.Font.Color = RGB(0, 0, 0)
    End With
    End With

End Sub

Sub TXNFont() will have error during first run, but will execute the code with no issue if I hit run again.

Sub NetFont() will execute the coding with error during first run, but will execute the code correctly if I hit run again.

These codes were link to another VBA code which was running fine with no issue. I hope that the codes I made run smoothly with no error during their first run. Thanks in advance people!

标签: excel vba
0条回答
登录 后发表回答