Using Offset in a For Each loop

Yeoman.jeremy

Board Regular
Joined
Apr 4, 2011
Messages
90
Hi,
I currently have this:
Code:
Private Sub CalcPUnit_Click()Dim rCell As Range
Dim rRng As Range


Worksheets("Inventory").Select
Set rRng = Range("E3:E50")


For Each rCell In rRng
rCell.Value = rCell.Offset(, 2) / rCell.Offset(, 1)
Next rCell
  
End Sub

Basically to say each cell in a range needs to become the value of 2 cells to the left divided by 1 cell to the left.
Whenever I run this code I get an overflow error.
Any ideas?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi,

If your amounts are " to the left " ...

You need to adjust your Offsets : Offset(,-2) and Offset(,-1)

HTH
 
Upvote 0
Hi, not correct :smile:
Also the problem wasn't with the directions it was something else :smile: thanks though

Feel free ... not to stay mysterious ... about THE solution you have found ...
 
Upvote 0
Try:
Code:
For Each rCell In rRng
If IsNumeric(rCell.Offset(, 2).Value) And rCell.Offset(, 1).Value <> 0 Then
rCell.Value = rCell.Offset(, 2) / rCell.Offset(, 1)
End If
Next rCell
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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