Subtracting same value from multiple cells (macro/VBA)

chrissy_v

New Member
Joined
Jul 18, 2022
Messages
16
Office Version
  1. 365
Platform
  1. MacOS
Hey I'm super new to macros and am trying to figure it all out, and I'm not entirely sure if what I'm trying to do is possible.
Basically I have a large inventory list with current stock of parts on hand. I'm trying to make a macro/VBA to subtract '1' from multiple cells. (When I make an assembly I'm deducting '1' of certain parts from the current list thus updating the current stock on hand). I've tried to do =J2-1 (for example) but that doesn't do anything when I try to run it as a macro.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Welcome to the Board!

Where are all these values, that you want to subtract one from, in relation to each other?

I think it would be very beneficial to us to see a sample of your data structure, and your expected output.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
We may need more details but with just your example it would be Range("J2") = Range("J2") - 1
 
Last edited:
Upvote 0
Welcome to the Board!

Where are all these values, that you want to subtract one from, in relation to each other?

I think it would be very beneficial to us to see a sample of your data structure, and your expected output.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Thank you!
So all of the parts the company uses are in one master list. All of the QOH are in the J column, and there's 254 rows in total. However depending on what we produce there will be different parts used. (for example one product could be using J2,6,10,8,5,etc and a different product could be J2,6,7,9,12,etc). Most of the final products we make use a lot of the same parts however there are some variations.
So my goal is to make multiple buttons to run macros for each final product that will subtract '1' from each part used, giving a running QOH.
 
Upvote 0
Ahhh thank you! To do multiple cells at once would I code it like Range("J2, J4, J5") = Range("J2, J4, J5") - 1 ?
Nope, that would not work. You cannot write it like that.
If you had a range that just contained cells J2, J4, and J5, you could loop through each cell in the range and apply the subtraction.

How is it determined which cells to include?
Is there any logic regarding which cells should be included and which should not (i.e. maybe something contained in another cell in the same row)?

That is why I was really hoping that you would post a sample of your data, that maybe if we saw it, it would be obvious to us how can identify those cells.
 
Upvote 0
Nope, that would not work. You cannot write it like that.
If you had a range that just contained cells J2, J4, and J5, you could loop through each cell in the range and apply the subtraction.

How is it determined which cells to include?
Is there any logic regarding which cells should be included and which should not (i.e. maybe something contained in another cell in the same row)?

That is why I was really hoping that you would post a sample of your data, that maybe if we saw it, it would be obvious to us how can identify those cells.
Inventory and Parts List 2022 with macros.xls
ABCDEFGHIJK
1CategoryDescriptionSupplierSupplier ContactSupplier NumberManufacturerManufacturer Num.PricePerStockValue
2BNCBNC bulkhead connectorMouserwww.mouser.com523-31-10-RFXAmphenol31-10-RFX$2.98145$134.10
3BNCBNC panel connectorMouserwww.mouser.com523-31-203RFXAmphenol31-203-RFX$4.7216$28.32
4Bumper Feet - PCAThreaded-Stud Bumper, SBR, 8-32 x 3/8"McMaster-Carrwww.mcmaster.com9541K2**$15.282533$20.17
5CableLiquid-tight cable connectors / strain reliefsIndustrial Devices770-935-4832PG9ACI106177$1.001151$151.00
6Cable - BNCBNC to BNC 2 ft. CALRADCable Depotwww.cabledepot.comBNC-BNC-RG59-2**$5.75130$172.50
7Cable - PowerPower cable 2.5m INTL 10AMouserwww.mouser.com562-363012Qualtek363012-D01$8.1013$24.30
8Cable - PowerPower cable 79" 18AWG NEMA 10AMouserwww.mouser.com562-311007-01Qualtek311007-01$4.65161$283.65
9Cable - PowerPower cable 90" 18AWG SVT 10ANewark InOnewww.newark.com98K6011Volex17250-B-1-10$5.27117$89.59
10Cable - Ribbon06" 20-pin 0.1" ribbon cableMouserwww.mouser.com517-1M-1010-020-63M1M-1010-020-3365-006.0-00-AB-00-0$8.50168$578.00
11Cable - Ribbon06" 60-pin 0.1" ribbon cableMouserwww.mouser.com517-1M-1010-060-63M1M-1010-060-3365-006.0-00-AB-00-0$13.25172$954.00
12Cable - Ribbon12" 16-pin 0.1" ribbon cableMouserwww.mouser.com517-1M-1010-016-123M1M-1010-016-3365-012.0-00-AB-00-0$8.72155$479.60
13Cable - Ribbon18" 16-pin 0.1" ribbon cableMouserwww.mouser.com517-1M-1010-016-183M1M-1010-016-3365-018.0-00-AB-00-0$9.38152$487.76
14Capacitor0.1uF 50V ceramic capacitorDigi-Keywww.mouser.com80-C317C104M5UKemetC317C104M5U5TA$0.131104$13.52
15Capacitor1uF 50V ceramic capacitorDigi-Keywww.mouser.com80-C330C105M5UKemetC330C105M5U5TA$0.301925$279.35
16Capacitor3300pF 50V ceramic capacitorMouserwww.mouser.com80-C315C332K5RKemetC315C332K5R5TA$0.14125$3.50
17Capacitor4.7uF 50V ceramic capacitorMouserwww.mouser.com80-C350C475M5UKemetC350C475M5U5TA$2.0810$0.00
18Cleaning BrushLow-Scratch Tube Brush, 1/2" Diameter x 3" LongMcMaster-Carrwww.mcmaster.com7221T12**$4.00116$64.00
Inventory
Cell Formulas
RangeFormula
D2:D3,D7:D8,D10:D17D2='Inventory and Parts List 2022 with macros.xls'!Mouser
D4D4='Inventory and Parts List 2022 with macros.xls'!McMaster
D5D5='Inventory and Parts List 2022 with macros.xls'!IndustrialDevices
D9D9='Inventory and Parts List 2022 with macros.xls'!Newark
K2:K18K2=(H2*J2)/I2
Named Ranges
NameRefers ToCells
BNCbulkhead=Inventory!$H$2K2
BNCcable=Inventory!$H$6K6
BNCpanel=Inventory!$H$3K3
Cap0.1uF=Inventory!$H$14K14
Cap1uf=Inventory!$H$15K15
Cap3300pF=Inventory!$H$16K16
Cap4.7uF=Inventory!$H$17K17
IndustrialDevices=Suppliers!$B$12D5
INTLcable=Inventory!$H$7K7
McMaster=Suppliers!$B$15D4
Mouser=Suppliers!$B$17D10:D17, D7:D8, D2:D3
NEMAcable=Inventory!$H$8K8
Newark=Suppliers!$B$18D9
RibbonDisplay=Inventory!$H$10K10
RibbonLeft=Inventory!$H$13K13
RibbonMain=Inventory!$H$11K11
RibbonRight=Inventory!$H$12K12
StrainRelief=Inventory!$H$5K5
SVTcable=Inventory!$H$9K9


I have included a small sample here. All of the parts in the master list are organized by what type of part they are, so there is no "rhyme or reason" for what I'm trying to do (because each final product uses all sorts of different parts).

I'm currently working on creating a list of parts with all corresponding cells for each final product.
 
Upvote 0
Nope, that would not work. You cannot write it like that.
If you had a range that just contained cells J2, J4, and J5, you could loop through each cell in the range and apply the subtraction.

How is it determined which cells to include?
Is there any logic regarding which cells should be included and which should not (i.e. maybe something contained in another cell in the same row)?

That is why I was really hoping that you would post a sample of your data, that maybe if we saw it, it would be obvious to us how can identify those cells.
They are all in the same column, but will have a wide variety of rows
 
Upvote 0
So, where exactly is the subtraction happening?
Can you walk us through an actual example, based on your data sample?
 
Upvote 0
So, where exactly is the subtraction happening?
Can you walk us through an actual example, based on your data sample?
In the J column (the current stock on hand).
(This example won't be completely accurate as there's over 250 rows) but let's say one product uses parts J3, J5, J9, J10, J11, J12, J13. So I would need to subtract '1' from those cells
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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