IF command + loop into VBA

krass

New Member
Joined
Nov 14, 2014
Messages
5
Help appreciated

I have a growing Worksheet (daily update) which has an IF command (in Col L) to calculate the difference between two cells in Col E - but only if value in Col A is identical. The IF command looks like this: =IF(A2<>A1;E2-0;E2-E1) and is placed in Col L

If the worksheet has been sorted eg. by Date, it would be nice to have it sorted by User number first, else, I believe nothing would work?


ABCDEFGHIJKL
1UserDistrictDateNumberConsumptionTemp1Temp2Temp3Temp4Temp5Diff
2100127-07-2018780173320,19932,5963,4330,4838,6-8,12-0,01620,199
3100128-07-2018780173320,20532,763,2730,6138,6-7,99-0,01640,006
4100129-07-2018780173320,20932,6263,3430,4938,6-8,11-0,01690,004
5100130-07-2018780173320,21532,663,3230,5138,6-8,09-0,0174
6100131-07-2018780173320,22332,7863,4230,638,6-8-0,0178
7100101-08-2018780173320,22632,6663,3630,5938,6-8,01-0,0181
8100102-08-2018780173320,23432,7763,3630,4638,6-8,14-0,019
9200101-07-2018781710360,01429,3660,9831,7139,2-7,49-0,001
10200102-07-2018781710360,02529,4560,2730,1439,5-9,36-0,0023
11200103-07-2018781710360,03929,4260,5329,8239,2-9,38-0,0037
12200104-07-2018781710360,05229,4260,5330,2639,2-8,94-0,0046
13200105-07-2018781710360,06929,9660,6130,339,2-8,9-0,0061
14200106-07-2018781710360,09130,4561,0930,3539,2-8,85-0,0081
15200107-07-2018781710360,11230,5761,5930,4838,9-8,42-0,0094
15200108-07-2018781710360,13531,0461,7630,4838,9-8,42-0,0114
17200109-07-2018781710360,15331,0361,7930,4238,9-8,48-0,013

<tbody>
</tbody>


Hope to get some help

Kim
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Thx Special-K99
Yes, gives me the same result but I still have to make sure the the code is in every row. I was hoping for a VBA code that could run automatically. The number of rows today are round 100.000 and added by apx 3000 rows daily.

Sorry :)
 
Upvote 0
How about
Code:
Sub AddFormula()
   Range("A1").CurrentRegion.Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
   Range("L2", Range("K" & Rows.Count).End(xlUp).Offset(, 1)).Formula = "=E2-IF(A2<>A1,0,E1)"
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Similar threads

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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