checking cell values beside selected range of cells

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi,
I have the following code and what I would like to do is after the user has selected a range of cells, when they run the macro I want it to check each cell value to the left of the selected cells. If the cell to the left of the cell contains "ABC" then I would like the selected cell to have the value of "123". I thought this might work but it doesn't seem to. If anyone could help me out that would be great!
Thanks.

Code:
Sub EnterRate()
    Dim Cell As Range
        For Each Cell In Selection
            If Cell.Offset(0, -1) = "ABC" Then
                Cell = "123"
            End If
        Next Cell
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You are pretty close. You should have a variable to represent the cell and another to represent the range. Then don't forget the ".Value" part.

Code:
Sub LoopRange()
      Dim rCell As Range
      Dim rRange As Range
      Set rRange = Selection
    
      For Each rCell In rRange.Cells
            If rCell.Offset(0, -1) = "ABC" Then
                  rCell.Value = "123"
            End If
      Next rCell
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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