簡體   English   中英

將條件格式公式應用於插入行中的單元格(無效的過程調用/參數運行時錯誤5)

[英]applying conditional formating formula to a cell in inserted row (invalid procedure call/argument run-time error 5)

我正在嘗試將條件格式設置公式應用於插入行的第二列。 如果用戶在單元格中輸入文本(如果單元格不是空白),則插入行B列中的單元格將更改顏色。

以下代碼產生運行時錯誤“ 5”(無效的過程調用/參數)。

謝謝,

Public Sub insertNewRow(ByVal Target As Range)
    Dim thisWs As Worksheet, thisRow As Range, l As Double, r As Double, i As Long
    Dim tr As Long

    'setColors
    Application.ScreenUpdating = False
    Set thisWs = ActiveSheet
    Set thisRow = Target
    tr = thisRow.Row
    If tr > 2 Then

        With Target.Cells(1, 2)
            .Select
            Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=(NOT(ISBLANK($B2))"
           Selection.FormatConditions(.FormatConditions.Count).Interior.Color = lBlu

            Target.Cells(1, 1).Value = Target.Cells(0, 1).Value 'copy down RunID/SampleID from row above
            With Target.Cells(1, 4)
                .Value2 = "New variant in row " & tr   'insert "New data" in first cell of current row
                .Interior.Color = vbYellow

            End With

        End With
        ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Activate
        thisWs.Activate 
        thisWs.Cells(1, 1).Select
    End If
    Application.ScreenUpdating = True
End Sub

公式是第一個問題,然后使用目標地址設置范圍

Public Sub insertNewRow(ByVal Target As Range)
    Dim thisWs As Worksheet, thisRow As Range, l As Double, r As Double, i As Long
    Dim tr As Long

    'setColors
    Application.ScreenUpdating = False
    Set thisWs = ActiveSheet
    Set thisRow = Target
    tr = thisRow.Row
    If tr > 2 Then

        With Target.Cells(1, 2)
           .FormatConditions.Add Type:=xlExpression, Formula1:="=NOT(ISBLANK(" & Target.Address & "))"
           .FormatConditions(.FormatConditions.Count).Interior.Color = lBlu

            Target.Cells(1, 1).Value = Target.Cells(0, 1).Value 'copy down RunID/SampleID from row above
            With Target.Cells(1, 4)
                .Value2 = "New variant in row " & tr   'insert "New data" in first cell of current row
                .Interior.Color = vbYellow

            End With

        End With
        ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Activate
        thisWs.Activate
        thisWs.Cells(1, 1).Select
    End If
    Application.ScreenUpdating = True
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM