Vba宏如果单元格包含值,则在其他单元格上输入特定文本

时间:2017-09-22 08:50:55

标签: excel vba excel-vba

我有一个excel文档,其中包含大量信息和统计信息,我试图弄清楚如何解决以下问题: 如果列E上的单元格在区间E5:E70中包含" N / A"文字(没有引号),我想自动输入" N / A"同一行中几个特定单元格上的文本

添加了参考图像image。 任何帮助将不胜感激,谢谢!

最新编辑: 我附上了excel的副本,也许它会更有帮助,对我来说它只是赢了工作......它太令人沮丧了...... excel file

3 个答案:

答案 0 :(得分:1)

将下面的代码粘贴到您要进行操作的工作表的代码表中。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim C As Long

    If Not Application.Intersect(Target, Range("E5:E70")) Is Nothing Then
        SetApplication False
        With Target
            If StrComp(Trim(.Value), "N/A", vbTextCompare) = 0 Then
                ' recognises "n/a" as "N/A" and corrects entry
                For C = Columns("E").Column To Columns("AL").Column
                    Cells(.Row, C).Value = "N/A"
                Next C
            End If
        End With
        SetApplication True
    End If
End Sub

Private Sub SetApplication(ByVal AppMode As Boolean)

    With Application
        .EnableEvents = AppMode
        .ScreenUpdating = AppMode
    End With
End Sub

答案 1 :(得分:1)

尝试

Sub Demo()
    Dim ws As Worksheet
    Dim cel As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")  'change Sheet1 to your data sheet
    For Each cel In ws.Range("E5:E70")
        If CVErr(cel.Value) = CVErr(xlErrNA) Then
            ws.Range("F" & cel.Row & ":I" & cel.Row) = CVErr(xlErrNA)   'mention desired range instead of (F:I)
        End If
    Next cel
End Sub

enter image description here

答案 2 :(得分:1)

我假设(基于图片)你想用这种方式粘贴N / A:F - 粘贴,G - 不粘贴,H - 粘贴 - 并进一步重复这三个:粘贴,不要粘贴,粘贴,粘贴,不粘贴,粘贴等。

因此,此代码适用于此规则。您只需要指定最后一列而不是Column.Count - 2 - 这一位说明该程序应该填写到表格中的最后一列。

Sub FillNAs()

Application.EnableEvents = False
Application.ScreenUpdating = False

Dim i, j As Long

For i = 5 To 70
    If UCase(Cells(i, 5).Value) = "N/A" Then
        j = 6
        Do While j < Columns.Count - 2
            Cells(i, j).Value = "N/A"
            Cells(i, j + 2).Value = "N/A"
            j = j + 3
        Loop
    End If
Next i

Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub