![](/img/trans.png)
[英]Put in a cell the sum of specific cells if other cells contains a specific text
[英]Vba macro if cell contains value, then input specific text on other cells
我有一个excel文档,其中包含大量信息和统计信息,我试图弄清楚如何解决以下问题:如果E列上的单元格,在区间E5:E70中,包含“N / A”文本(如果没有引号,我想在同一行的几个特定单元格中自动输入“N / A”文本
最新编辑:我附上了excel的副本,也许它会更有帮助,对我来说它只是行不通...它太令人沮丧了... excel文件
将下面的代码粘贴到您要进行操作的工作表的代码表中。
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
尝试
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
我假设(基于图片)你想要用这种方式粘贴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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.