![](/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.