VBA to find values and reduce cell X by cell Y

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
387
Hi All

I'm sure there is a simple piece of VBA to do this but can't work out how.

For example, If column A has a list of letters, Column B a number, and C a greater number i.e.

Column A Column B Column C
A 2 10
B 4 8
C 6 14
D 8 20
E 10 18
A 1 19
B 3 7
C 5 11
D 7 13
E 9 9

Is it possible to locate the letter B, reduce the value in column C by the value in column B and then delete the value from column B. Actual data is 30 thousand rows long hence the need to automate it via VBA.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

nuked

Well-known Member
Joined
Mar 20, 2013
Messages
883
Code:
Sub ReduceStock()

Dim rng as Range
Dim cel as Range

Set rng = Activesheet.Range("A1:A" & ActiveSheet.Range("A" & Activesheet.Rows.Count).End(xlUp).Row)

For each cel in rng

If trim(cel) = "B" then

cel.Offset(,2) = cel.offset(,2)-cel.offset(,1)
cel.offset(,1) = ""

end if

next cel

end sub
 

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
387
Code:
Sub ReduceStock()

Dim rng as Range
Dim cel as Range

Set rng = Activesheet.Range("A1:A" & ActiveSheet.Range("A" & Activesheet.Rows.Count).End(xlUp).Row)

For each cel in rng

If trim(cel) = "B" then

cel.Offset(,2) = cel.offset(,2)-cel.offset(,1)
cel.offset(,1) = ""

end if

next cel

end sub

Perfect, thank you so much :biggrin:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,095
Messages
5,835,356
Members
430,351
Latest member
ddalton

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
Top