仅在首次运行期间出现Excel VBA错误代码

时间:2019-01-18 09:29:48

标签: excel vba

我尝试了几种方法来消除我的VBA错误,但这无济于事。 在第一次运行期间,将出现错误代码9。再次运行时,该代码可以正常工作。我有另一个没有错误的代码,但第一次运行时未正确执行VBA代码。再次运行代码后,它将正确执行VBA代码。

我尝试使用F8调试问题,并且尝试了循环。

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()在第一次运行时会出错,但是如果我再次点击运行,它将执行代码没有问题。

Sub NetFont()将在第一次运行时执行错误的编码,但是如果再次点击运行将正确执行代码。

这些代码已链接到另一个运行正常的VBA代码,没有任何问题。我希望我编写的代码在第一次运行时能顺利运行且没有错误。预先感谢大家!

0 个答案:

没有答案