I am new to VBA, and I am starting to do some basic coding.
My current end goal is to allow a cell in a table to be zeroed out based on the inputs of two data validated cells.
I currently have a sheet where I have managed to successfully implement what I am trying to do, but the code is lengthy and I am sure it can be streamlined by using the Find function. I tried to implement Find for a few hours before implementing this workaround.
Users select the current Forecast Month through a validated list in cell B4. Then they can choose to "zero out" the desired cell by selecting yes/no in cell B5. Months are laid out from B7:M7. My original goal was to have VBA take the selected month from B4, find that month in B7:M7, return that column, and then using the column data go to row 15 and zero out that cell. However, I could not find the appropriate way to code this into the find function without causing a compile error.
Below is the current code that I believe can be streamlined:
Private Sub Worksheet_Change_B(ByVal Target As Range)
If Target.Address(False, False) = "B5" Then
If Range("B5").Value = "Yes" Then
If Range("B4").Value = "Oct" Then
Range("B15").Value = "0"
ElseIf Range("B4").Value = "Nov" Then
Range("C15").Value = "0"
ElseIf Range("B4").Value = "Dec" Then
Range("D15").Value = "0"
ElseIf Range("B4").Value = "Jan" Then
Range("E15").Value = "0"
ElseIf Range("B4").Value = "Feb" Then
Range("F15").Value = "0"
ElseIf Range("B4").Value = "Mar" Then
Range("G15").Value = "0"
ElseIf Range("B4").Value = "Apr" Then
Range("H15").Value = "0"
ElseIf Range("B4").Value = "May" Then
Range("I15").Value = "0"
ElseIf Range("B4").Value = "Jun" Then
Range("J15").Value = "0"
ElseIf Range("B4").Value = "Jul" Then
Range("K15").Value = "0"
ElseIf Range("B4").Value = "Aug" Then
Range("L15").Value = "0"
ElseIf Range("B4").Value = "Sep" Then
Range("M15").Value = "0"
End If
ElseIf Range("B5").Value = "No" Then
If Range("B4").Value = "Oct" Then
Range("B15").Value = Range("B35").Value
ElseIf Range("B4").Value = "Nov" Then
Range("C15").Value = Range("C35").Value
ElseIf Range("B4").Value = "Dec" Then
Range("D15").Value = Range("D35").Value
ElseIf Range("B4").Value = "Jan" Then
Range("E15").Value = Range("E35").Value
ElseIf Range("B4").Value = "Feb" Then
Range("F15").Value = Range("F35").Value
ElseIf Range("B4").Value = "Mar" Then
Range("G15").Value = Range("G35").Value
ElseIf Range("B4").Value = "Apr" Then
Range("H15").Value = Range("H35").Value
ElseIf Range("B4").Value = "May" Then
Range("I15").Value = Range("I35").Value
ElseIf Range("B4").Value = "Jun" Then
Range("J15").Value = Range("J35").Value
ElseIf Range("B4").Value = "Jul" Then
Range("K15").Value = Range("K35").Value
ElseIf Range("B4").Value = "Aug" Then
Range("L15").Value = Range("L35").Value
ElseIf Range("B4").Value = "Sep" Then
Range("M15").Value = Range("M35").Value
End If
End If
End If
End Sub
What I was originally thinking with the Find Function went like this, but I could not get it to compile properly.
Sub Test()
Dim rFind As Range
If Target.Address(False, False) = "B5" Then
If Range("B5").Value = "Yes" Then 'If the user wants to zero out the cell on row 15
With Range("B7:M7") 'Range of Months
Set rFind = .Find(What:=("B4"), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Range("rFind.Column,15").Value = "0" 'Set Value of cell in row 15 of referenced column to zero
End With
If Range("B5").Value = "No" Then 'If the user doesn't want to zero out cell on row 15
With Range("B7:M7") 'Range of Months
Set rFind = .Find(What:=("B4"), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Range("rFind.Column,15").Value = Range("rFind.Column,35").Value 'Pulls in Previous Value
End With
End If
End Sub
Am I getting on track? How can I reference the month column in the cell address I would like to change?