Is there a way to shorten this?

T

TyeJae

I have to go up to 20 on my If statement here and was wondering if ther
was a way to shorten this in VBA.

Example:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("C6").Value = 0 Then
Range("M6:AF6").Value = "-"
End If
If Range("C7").Value = 0 Then
Range("M7:AF7").Value = "-"
End If
If Range("C8").Value = 0 Then
Range("M8:AF8").Value = "-"
End If
If Range("C9").Value = 0 Then
Range("M9:AF9").Value = "-"
End If
If Range("C10").Value = 0 Then
Range("M10:AF10").Value = "-"
End If
If Range("C11").Value = 0 Then
Range("M11:AF11").Value = "-"
End If
If Range("C12").Value = 0 Then
Range("M12:AF12").Value = "-"
End If
If Range("C13").Value = 0 Then
Range("M13:AF13").Value = "-"
End If
If Range("C14").Value = 0 Then
Range("M14:AF14").Value = "-"
End If
If Range("C15").Value = 0 Then
Range("M15:AF15").Value = "-"
End If
If Range("C16").Value = 0 Then
Range("M16:AF16").Value = "-"
End If
If Range("C17").Value = 0 Then
Range("M17:AF17").Value = "-"
End If
If Range("C18").Value = 0 Then
Range("M18:AF18").Value = "-"
End If
End Sub

Hopefully there is, otherwise I am going to be busy.

-TyeJae
 
P

pikus

Private Sub Worksheet_SelectionChange(ByVal Target As Range)for x = 6 T
18
If Cells(x, 3).Value = 0 Then
Range(.Cells(x, 13), .Cells(x, 32)).Value = "-"
End If
End Sub

I didn't test this, but it works in theory. - Piku
 
T

TyeJae

I could not get that to work, I copied and pasted exactly what you pu
in, am I supposed to substitute anything?

-TyeJae
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top