VBA to subtract Cell A value from Cell B value until Cell B is 0, BUT.........

idontknowhow

New Member
Joined
Aug 11, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all! I need some real big help, as this has been driving me crazy.

I have 28 columns, with Column R containing a number (ie R2). I need to subtract the R2 value number from a cell that has a total value number in AB2. After Row 2 has R2 subtracted from AB2, I need to copy/paste that whole row to another sheet, delete Row 2, then repeat (loop) that process for Row 3, 4, 5, etc.. until AB2 equals 0 from the continuous subtraction from Column R values.

Any help or suggestions would be appreciated!!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Welcome to the Forum!

Sorry you've had no responses. Usually that indicates a question is not clear, or it's too complicated.

I'm far from clear what you're asking. But here's my guess based on what you've said so far. Cell AB2 contains a number, let's say 32. In Column R you have these values, say:

Book
R
1
2
3
4
55
62
77
88
96
104
119
1212
Sheet1


You want your code to copy rows 5:10 to another sheet (and delete them in this sheet) because these R values add to 32, i.e. if you consecutively took them away from AB2, you'd be left with nil?

Bearing in mind that we have no idea what's in your workbook, it's always helpful if you can post a screenshot showing sample data, and the results you'd like to see, preferably using the XL2BB add-in, so that we can copy directly into a test workbook.
 
Upvote 0
Sorry, probably having an attachment would work better to explain. So I need to start at Row2/Col B (hours), subtract that 'hour value' from Row1/Col E (200), then copy/paste that Row to another sheet, then delete that Row on this sheet, then continue through that process down the Rows until 200 is 'nil'.

In this example, Data 1-17 and 21 would be used, since they total 200. Data 1-17 equals 193, so the macro would skip Data 18-20, then chose Data 21 to make the total 'nil'.

Column A (Data)Column B (Hours)Column C (Details)Column D200
Data 113Details
Data 212Details
Data 311Details
Data 49Details
Data 512Details
Data 611Details
Data 714Details
Data 89Details
Data 910Details
Data 1012Details
Data 1112Details
Data 1212Details
Data 1311Details
Data 1411Details
Data 1510Details
Data 1612Details
Data 1712Details
Data 1812Details
Data 1912Details
Data 2012Details
Data 217Details

Thanks again for looking at this!
 
Upvote 0
Sorry, probably having an attachment would work better to explain ....
Yes, thanks for that.

You've given us one simple example, but I suspect the possibilities may be more complex.

Consider the following:

ABCDEFGHI
1RequiredOption 1Option 2Option 3
2DataHoursDetails60606060
3Data 110101010
4Data 210101010
5Data 310101010
6Data 410101010
7Data 510101010
8Data 688
9Data 755
10Data 855
11Data 91010
12Data 1010
13Data 1110
14Data 1222
15Data 1310
16Data 1410
Sheet1
Cell Formulas
RangeFormula
G2:I2G2=SUM(G3:G16)

Using your approach, Option 1 would be the solution. But if it's a requirement to take Data1, Data2, Data 3 ... in order, perhaps Option 2 or 3 would be preferred?

Also, what happens if it's not possible to exactly add to the required amount?

With VBA, the code will need to know exactly how to behave in all these possible circumstances. This could get complicated, and require more time than a volunteer forum is willing to put in.
 
Upvote 0

Forum statistics

Threads
1,215,525
Messages
6,125,325
Members
449,218
Latest member
Excel Master

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