VBA to find values and reduce cell X by cell Y

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
382
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.
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

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
382
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:
 

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,474
Members
414,070
Latest member
DuncanLucas

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