VBA - Make same formulas into a single one

The Godfather

New Member
Joined
Jul 22, 2011
Messages
30
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I have the following formula below, where a single task is done for 10 different rows (stars with row 12 and ends with row 21).
I typed the code for each row. Is it possible to do it just with a single code? (I tried ranging the cells - ie ranging "I12:J12"- but it crashed)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Range("I12"), Range(Target.Address)) Is Nothing Then
Range("J12").Value = ""
Range("K12").Value = ""
End If

If Not Intersect(Range("I13"), Range(Target.Address)) Is Nothing Then
Range("J13").Value = ""
Range("K13").Value = ""
End If

If Not Intersect(Range("I14"), Range(Target.Address)) Is Nothing Then
Range("J14").Value = ""
Range("K14").Value = ""
End If

If Not Intersect(Range("I15"), Range(Target.Address)) Is Nothing Then
Range("J15").Value = ""
Range("K15").Value = ""
End If

If Not Intersect(Range("I16"), Range(Target.Address)) Is Nothing Then
Range("J16").Value = ""
Range("K16").Value = ""
End If

If Not Intersect(Range("I17"), Range(Target.Address)) Is Nothing Then
Range("J17").Value = ""
Range("K17").Value = ""
End If

If Not Intersect(Range("I18"), Range(Target.Address)) Is Nothing Then
Range("J18").Value = ""
Range("K18").Value = ""
End If

If Not Intersect(Range("I19"), Range(Target.Address)) Is Nothing Then
Range("J19").Value = ""
Range("K19").Value = ""
End If

If Not Intersect(Range("I20"), Range(Target.Address)) Is Nothing Then
Range("J20").Value = ""
Range("K20").Value = ""
End If

If Not Intersect(Range("I21"), Range(Target.Address)) Is Nothing Then
Range("J21").Value = ""
Range("K21").Value = ""
End If

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Im not an expert but I have been playing with a similar thing recently, can you try this and see if it works for you?

Ive only done the first 5, you can add more into the ranges using commas until you have all, if it works.

VBA Code:
If Not Intersect(Range("I12, I13, I14, I15, I16"), Range(Target.Address)) Is Nothing Then
Range("J12, K12, J13, K13, j14, k14, j15, k15").Value = ""
End If
 
Upvote 0
MAybe this way
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
For r = 12 To 21
If Not Intersect(Range("I" & r), Range(Target.Address)) Is Nothing Then
Range("J" & r).Value = ""
Range("K" & r).Value = ""
End If
Next r
End Sub
 
Upvote 0
Solution
Im not an expert but I have been playing with a similar thing recently, can you try this and see if it works for you?

Ive only done the first 5, you can add more into the ranges using commas until you have all, if it works.

VBA Code:
If Not Intersect(Range("I12, I13, I14, I15, I16"), Range(Target.Address)) Is Nothing Then
Range("J12, K12, J13, K13, j14, k14, j15, k15").Value = ""
End If
Thank you grabrail, I tried your solution. Infortunately if I change only one cell in I column, all cells in J and K columns become empty.
 
Upvote 0
MAybe this way
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
For r = 12 To 21
If Not Intersect(Range("I" & r), Range(Target.Address)) Is Nothing Then
Range("J" & r).Value = ""
Range("K" & r).Value = ""
End If
Next r
End Sub
Thank you Michael M, it worked.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top