excelvba

how do I make multiple specific cells in a row reset number to 0 if specific cell in the same row= text, applied to all rows


(I have almost 0 vba knowledge)

example

if I have 5 rows and B, D, F, L= a number for each row if specific text is in cell M for 2 specific rows the command resets all the numbers in the specific row and column to 0

so B2, B5, D2, D5, F2, F5, L2, L5 now equal 0 because M2, M5= specified text

I tried looking into it this is what I found and slightly modified

Sub Brexlin()

    [F2,H2,J2,L2,N2,U2,V2] = [if(AA2="MIA",0,)]

End Sub

that applies to 1 row but how do I make something like this apply to all rows

one person told me this "Basically just an if statement within a change event" but idk what to make of it with my lack of knowledge


Solution

  • If my understanding is correct, the next solution is for you:

    enter image description here

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim stext$
      stext = "specific text"
      If Intersect(Target, Range("M2, M5")) Is Nothing Then Exit Sub
      Application.EnableEvents = False
      If [M2] = stext And [M5] = stext Then Range("B2, B5, D2, D5, F2, F5, L2, L5") = 0
      Application.EnableEvents = True
    End Sub
    

    If you change M2 or M5 of Sheet1 and both M2 and M5 contains the "specific text" (change on your own) zero writes to cells B2, B5, D2, D5, F2, F5, L2, and L5.

    To do this press Alt+F11 in Excel. VBA editor opens. In the VBA Project browser on the left side, double-click on the sheet you need. The blank sheet opens (should be blank in the new sheet/workbook) on the right side. Copy and paste the code. Come back to Excel and test: enter the specific text into M2 then M5.

    UPD

    The next code processes all rows where "dispose" is entered to M (change it if required in two places). The columns to reset is defined in the "ctd" array.

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim stext$, ctd, ca, cc, cs
      If Intersect(Target, Range("M:M")) Is Nothing Then Exit Sub
      stext = "dispose"
      ctd = Array("B:B", "D:D", "F:F", "L:L")
      Application.EnableEvents = False
      If IsArray(Target) Then
        For Each ca In Target.Areas
          For Each cc In Intersect(ca, Range("M:M"))
            If cc = stext Then
              For Each cs In ctd
                Range(cs).Cells(cc.Row) = 0
              Next cs
            End If
        Next cc, ca
      ElseIf Target = stext Then
        For Each cs In ctd
          Range(cs).Cells(Target.Row) = 0
        Next
      End If
      Application.EnableEvents = True
    End Sub
    

    UPD

    For the last comment "say I use a formula to keep column M updated from a different sheet, how do I make it read the change in sheet 1 I type MIA which reflects in Sheet 2 column M which clears the data in sheet 2", you need to handle the Calculate event on the target sheet (you say Sheet 2):

    Private Sub Worksheet_Calculate()
      Dim stext$, ctd, ca, cc, cs, Target As Range
      Set Target = Intersect(Range("M:M"), Me.UsedRange)
      stext = "dispose"
      ctd = Array("B:B", "D:D", "F:F", "L:L")
      Application.EnableEvents = False
      If IsArray(Target) Then
        For Each cc In Target.Cells
          If cc = stext Then
            For Each cs In ctd
              Range(cs).Cells(cc.Row) = 0
            Next
          End If
        Next
      ElseIf Target = stext Then
        For Each cs In ctd
          Range(cs).Cells(Target.Row) = 0
        Next
      End If
      Application.EnableEvents = True
    End Sub