Code only looping through cells in first row of the range

Matt888

New Member
Joined
Dec 16, 2021
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hi,

The following code is intended to apply a formula to each cell in a defined range, then paste as values, then loop through each cell in the range twice to do 2 things:-

1. clear the contents of any cells where the formula result was "" - this loop work perfectly
2. for each non empty cell, if it equals the next non empty cell to the right then select the range between both non empty cells and apply fill color. Then clear the contents of the matching non empty cell to the right. - this loop only works for the first row in my range

Any help getting that 2nd loop to work would be greatly appreciated. Code below:-

Rich (BB code):
Sub CalcCalendar()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim rng As Range: Set rng = Range("Calendar_Range")
Dim cel As Range

Sheets("Calendar").Select
rng.UnMerge
Range("C7").Formula = "=IFERROR(XLOOKUP($B7&C$1,Prod_Range&Start_Date_Range,Desc_Range),IFERROR(XLOOKUP($B7&C$1,Prod_Range&End_Date_Range,Desc_Range),""""))"
Range("C7").Copy Destination:=rng
rng.Copy
rng.PasteSpecial xlPasteValues

For Each cel In rng.Cells
If cel = "" Then
cel.ClearContents
End If
Next cel

For Each cel In rng.Cells
If cel <> "" And cel.Value = Selection.End(xlToRight).Value Then
cel.Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Interior.Color = 65535
cel.Select
Selection.End(xlToRight).Select
Selection.ClearContents
cel.Select
End If
Next cel

Range("C1").Select

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I am not convinced the code is going to do what you are trying to do and we would not normally use "Select" in the code but in terms of your immediate issue add the line in Blue into your code.
Rich (BB code):
Set rng = Range("A3:D6")
For Each cel In rng.Cells
    cel.Select
    If cel <> "" And cel.Value = Selection.End(xlToRight).Value Then
 
Upvote 0
Solution
I am not convinced the code is going to do what you are trying to do and we would not normally use "Select" in the code but in terms of your immediate issue add the line in Blue into your code.
Rich (BB code):
Set rng = Range("A3:D6")
For Each cel In rng.Cells
    cel.Select
    If cel <> "" And cel.Value = Selection.End(xlToRight).Value Then
Thanks. I know it's a bit unconventional but seems such a simple thing I'm trying to achieve. Your correction actually seems to have done the trick. thanks for your help
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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