Forum Discussion
stephxian
Jan 11, 2023Copper Contributor
VBA Code to Multiply a range of cells by a reference cell
Hello,
I am creating a form for internal use for our employees. I am trying to make it as user friendly as possible for them, as they have varying levels of excel knowledge.
There is a range of columns (K8:K28) that need to be multiplied by a percentage. The percentage is different for each project, so the form is set up with a formula (=1-M8) to calculate this percentage based on the data. This reference cell is K7.
What I am hoping for, is possibly some VBA code that will automatically multiply cell K7 when a value is entered into one of the cells in the range (K8:K28). Is this possible?
Thank you for your help!
- mathetesSilver Contributor
What I am hoping for, is possibly some VBA code that will automatically multiply cell K7 when a value is entered into one of the cells in the range (K8:K28). Is this possible?
Most things like this are possible. What's not clear is why you're specifying VBA code at this point. VBA and macros are sometimes needed, to be sure. What you've described doesn't yet sound like one of the situations that require one of them. And for novice users in particular, VBA/macro routines can make things decidedly unfriendly. So may we put that assumption aside for the moment.
But it's also not totally clear what you're asking. The formula =1-M8, for instance, makes no reference to cell K7. Or is that value, the value of 1-M8, what is in cell K7? How, in other words, do those two components fit together in the scenario of an entry being made in the range K8:K28?
So let's assume I make an entry in cell K20 ... Please describe in words what is supposed to happen involving the ingredients named above.
Where is the result of whatever multiplication takes place to appear?
Is it possible (without violating any proprietary or confidential info) for you to post a copy of the spreadsheet as it currently exists, in OneDrive or GoogleDrive, pasting a link here granting edit access to it?
- stephxianCopper Contributor
Thank you for your reply. Yes, the value in K7 is the formula =1-M8. If you were to enter a value into K20, I want that cell to automatically display the value of K20*K7.
I am certainly open to other ways to accomplish this other than VBA. I went down that road because other options seemed to required that the data already be filled in or required that K7 is a real number and not a formula. I am the only one setting up the form using the code. The other users will only be inputting the data. They will not be messing with any coding or formulas. I am hoping to get the range to calculate for them automatically when they enter the data because some may not do well having to enter the formula themselves for each data entry. Does that make sense?
Thank you for your time.
- mathetesSilver Contributor
If you enter a value into cell K20, it would override any formula in K20. So that calculation needs to be in some other cell. There are multiple ways to do that.
I've attached a spreadsheet with an example of one way. There are two sheets, one for Entry, the other for Output. Comments are on the sheet explaining in a bit more detail. Feel free to come back with any questions.