VBA code to subtract two columns using loop

FROGGER24

Well-known Member
Joined
May 22, 2004
Messages
704
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Would like help in troubleshooting my code for a loop.
this is what i have tried so far. I am getting error 'Run time 1004 -Method range of object _Global failed.

<Sub subtract_col()
Dim LR As Long, i As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To LR
Cells(i, 10).Value = Range(i, 24).Value - Range(i, 26).Value
Next i
End Sub>

If i use the code like this there is no error
Range("J2").Value = Range("x2").Value - Range("z2").Value
Thanks for you help and time!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You just need to change the Range to Cells
 
Upvote 0
Solution
Another option without a loop
VBA Code:
Sub trimmer()
   With Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("if({1}," & .Offset(, 14).Address & "-" & .Offset(, 16).Address & ")")
   End With
End Sub
 
Upvote 0
Sub trimmer() With Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row) .Value = Evaluate("if({1}," & .Offset(, 14).Address & "-" & .Offset(, 16).Address & ")") End With
Sub trimmer() With Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row) .Value = Evaluate("if({1}," & .Offset(, 14).Address & "-" & .Offset(, 16).Address & ")") End With End Sub
when I tried the last formula, I get error #VALUE!, for my understanding can you explain the formula... thanks
 
Upvote 0
The evaluate function calculates a formula that is passed to it as a text string, and the formulae is effectively
Excel Formula:
=IF({1},X2-Z2)
where the {1} returns true so that it calculates the X2-Z2
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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