Repeat existing macro for certain rows?

Mikoustics

New Member
Joined
Jan 11, 2011
Messages
6
Hi folks,

I have a macro to update cells C18 and D18 to be blank when a choice in a data validation list in B18 is changed (refreshing an INDIRECT list in cell C18 related to a B18 data validation list to blank and to a blank cell in D18). I would like this macro to be repeated for the whole cell ranges C18:C37 and D18:D37 when its adjacent choice cell (B18:B37) is changed.

e.g. If the user chooses an option from the data validation list in cell B21, I would like only cells C21 and D21 to refresh to blank.

Cheers for any help! Here is the code:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngParentCell As Range
Dim rngDepCell As Range
Dim rngCell As Range

Set rngParentCell = Range("B18")
Set rngDepCell = Intersect(Target, rngParentCell)

If Not rngDepCell Is Nothing Then
Set rngCell = Range("C18:D18")
rngCell.ClearContents
rngCell.Select
MsgBox _
Title:="Notice", _
Prompt:="Please select 'Type', 'Length of Duct (m) and 'No. (Default 1)'", _
Buttons:=vbInformation + vbOKOnly
Application.SendKeys ("%{DOWN}")

End If

Set rngParentCell = Nothing
Set rngDepCell = Nothing
Set rngCell = Nothing

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You could try changing:
Code:
Set rngParentCell = Range("B18")
to:
Code:
Set rngParentCell = Range("B18:B37")
And:
Code:
Set rngCell = Range("C18:D18")
to:
Code:
Set rngCell = Range("C" & rngDepCell.Row & ":D" & rngDepCell.Row)

However, if more than one cells in the target range changes at the same time, e.g. B18 and B19 both change at the same time then the above code will only blank out cells for the first row, i.e. C18 and D18.

But I'm assuming that this wouldn't be a problem because you're using data validation lists so only one cell is expected to change at a time.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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