使用 VBA 添加行并用文本填充列

问题描述 投票:0回答:1

我需要在填写任何行(甚至部分)后添加一行,并在单元格为“否”时自动插入警告。 在“是否经过认证?”一栏下如果单元格为“否”,它会自动着色,并且旁边会出现警告。 我尝试添加条件格式,但对于使用宏创建的新行,即使单元格为“是”,也会保留此格式。 这是我到目前为止所拥有的,它适用于添加行。在下面的文字中...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
     If Target.Column = 3 Then
        r = Target.Row + 1
        Rows(r).Insert xlShiftDown, xlFormatFromLeftOrAbove
        Cells(r, 1).Select
     End If
     If Target.Column = 5 Then
        r = Target.Row + 1
        Rows(r).Insert xlShiftDown, xlFormatFromLeftOrAbove
        Cells(r, 1).Select
     End If
End Sub

Sub InsertFormula(ByVal cell As Range)
     Dim cell As Range
     Sheets("Sheet1").Select
     For Each cell In Range("F12:F29")
          If cell.Value = "No" Then
          Range("I12:I29").Select
          Selection.Formula = "IT CAN T BE USED"
          Selection.Colums.AutoFit
          End If
    Next cell
End Sub

我需要在填写任何行(甚至部分填写)后添加一行,并在单元格为“否”时自动插入警告。

excel vba addition conditional-formatting
1个回答
0
投票

您的 Worksheet_Change 事件似乎缺少检查逻辑。为了确保仅当用户在 C 列中输入“否”时才插入行,我将修改代码如下,此代码确保仅当在 C 列或 E 列中输入“否”时才插入新行。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
     If Target.Column = 3 Then
        If Target.Value <> "No" Then Exit Sub
        r = Target.Row + 1
        Rows(r).Insert xlShiftDown, xlFormatFromLeftOrAbove
        Cells(r, 1).Select
     End If
     If Target.Column = 5 Then
        If Target.Value <> "No" Then Exit Sub
        r = Target.Row + 1
        Rows(r).Insert xlShiftDown, xlFormatFromLeftOrAbove
        Cells(r, 1).Select
     End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.