VBA: Clear Contents for Multiple Groups of Columns based on Data

VBA Beginner 2024

New Member
Joined
Jan 30, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to use VBA to clear contents of a group of cells in a row based on data in various columns:
Example:
1706657783075.png


IF Column A or G is Y i want to clear contents of B to F or H to L respectively.
I have the following using different columns but for example in row 3, it would delete data in B to F instead of H to L.

Dim lr As Long, i As Long
lr = Cells(Rows.Count, "X").End(xlUp).Row
For i = 2 To lr
If UCase(Cells(i, "X")) = "Y" Then Cells(i, "Y").Resize(1, 5).ClearContents
Next i
lr = Cells(Rows.Count, "AC").End(xlUp).Row
For i = 2 To ls
If UCase(Cells(i, "AC")) = "Y" Then Cells(i, "Y").Resize(1, 5).ClearContents
Next i

How would i write the second part to focus on 2nd set of columns vs the first?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hmmm, so if I understand this correctly, if Column A is Y then clear content on columns H:L but if Column G is Y then clear content on columns B:F? Is that accurate?
 
Upvote 0
Hmmm, so if I understand this correctly, if Column A is Y then clear content on columns H:L but if Column G is Y then clear content on columns B:F? Is that accurate?
yeah i forgot to change the second Y to the starting point of the next deleted columns. Thank you for replying!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

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