嗨我正在處理下面的代碼,但它顯示錯誤,當我嘗試從工作表中一次刪除整個列(多個單元格)。該代碼與if語句正常工作,只是我不能從表單中一次刪除所有「M」&「我」字符。 下面是代碼:vba if語句錯誤
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 18 Then
ThisRow = Target.Row
If Target.Value = "M" Then
Range("AB" & ThisRow) = "NA"
ElseIf Target.Value = "I" Then
Range("AB" & ThisRow) = ""
Else
Range("AB" & ThisRow) = ""
End If
If Target.Value = "M" Then
Range("AC" & ThisRow) = "NA"
ElseIf Target.Value = "I" Then
Range("AC" & ThisRow) = ""
Else
Range("AC" & ThisRow) = ""
End If
End If
End Sub
更新代碼
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Integer, lastRow As Integer
If (Not Target.Column = 24) Then Exit Sub
If (Target.Columns.Count > 1) Then Exit Sub
lastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
Application.ScreenUpdating = False
For r = Target.Row To Target.Row + Target.Rows.Count - 1
Range("Z" & r & ",AA" & r & ",AB" & r & ",AC" & r) = IIf(Cells(r, 24) = "Yes", "NA", "")
Range("Y" & r & ",Z" & r & ",AA" & r & ",AB" & r & ",AC" & r & ",AD" & r) = IIf(Cells(r, 24) = "No", "NA", "")
If (r = lastRow) Then Exit Sub
Next r
Application.ScreenUpdating = True
末次
什麼是拋出的錯誤,哪條線導致它? – RGA
這條線('ElseIf Target.Value =「I」Then')沒有目的;你的代碼表明,如果目標單元格的值是M,則將AB/AC替換爲「NA」,否則將其替換爲「」。如果Else的輸出是同一個東西,那麼你不需要'ElseIf'。 – Dave