Plus, is it worth using it?
Hard coding sounds like a complex programming concept, but it’s a fundamental concept in Microsoft Excel. Let’s learn what it is and when you should use it.
The concept is familiar to those well-versed in a programming language — just replace cells with variables that hold specific values, and the functional meaning remains the same.
A Crash Course in Cell References
To understand hard coding, we need to know how Excel cell references work because that’s where it all starts.
Every cell in Excel can be referenced by combining its alphabetical column number and its numerical row number. This lets you indicate specific cells with notations like A3, D5, etc.
Instead of individual cells, you can reference entire ranges with a colon. Typing B4:B12, for example, gives you all the cells in that range. You can even link sheets between different sheets and workbooks with these relationships.
Almost all mathematical operations on the cells are performed using their cell references, which is more convenient than looking up and entering the actual values. There is another reason, but more on that in the next section.
Why Is It Recommended to Use Cell References in Excel?
The premise of using Excel is the convenience of updating values due to its formulas. Say you have a spreadsheet calculating a company’s total expenses in a month and are using that total in many other graphs and calculations.
With pen-and-paper, you will have to recalculate the total and all the other calculations that were using its value, which is a tedious and error-prone process. Not to mention near impossible if the spreadsheet has thousands of entries in it.
But in Excel, as long as you use the correct cell references, you don’t have to worry about fixing calculations. Change any value you need to, and all the calculations referencing that cell will automatically update their results (though you might have to use the Calculate Sheet button).
What Is Hard Coding? When Should You Use It?
Hard coding in Excel is when you ditch cell references in favor of absolute values. This means no matter what changes take place with the cells or their contents, the hard-coded values remain the same.
Generally, it is not a good idea to use hard-coded values. Poorly planned out hard coding is how complex spreadsheets get broken since it is a pain to hunt down the hard-coded values and change them should the need arise.
But, there are certain situations where hard coding is practical. Things like constants or historical data are values that you want to keep the same based on their references.
For such numbers, we use hard coding in Excel. Here are some ways of doing it.
Methods of Hard Coding
Manually Entering Values (Not Recommended)
The easiest (and most inadvisable) way of hard coding is manually entering the numbers you need. This means not using cell references but simply typing in the values.
You should only use this method for string values since they are not meant to be used in calculations. And, of course, the first time you enter any values in your spreadsheet is also technically hard coding.
Just remember to use references in any formula using these base values.
Define a Constant (Using Name Manager)
The more elegant way of hard coding is by defining constant values. Excel provides the Name Manager for doing just this.
The idea is to give your hard-coded value a name in the spreadsheet so that it is clear what the role of the value is, besides giving you a central location from which to change it. This is a lot like how constants function in many programming languages.
Though you might be wondering: what’s the point of using Define Name when you can get similar results by entering the value into a dedicated cell and referencing it? The only difference is that the name more clearly signifies what the value represents.
You can define an entire range of values with this method, apart from creating named constants that don’t exist on the spreadsheet.
- To define your own constants, select the Formulas tab in Excel and click on the Name Manager icon.
- If any cells are selected, the Name Manager window will give the values a name based on the context (usually the row or column label) and create a name for you. You can also click the New button to define your names and values from scratch.
- Now you can use this data in any of your formulas using the defined name. It acts as a more descriptive cell reference.
With the Paste Special Command
Usually, when you copy a selection of cells containing formulas, Excel automatically adjusts the references to their new location. But what if you are copying over an old dataset and only need their original values as it is?
This is where the Paste Special command comes in. Paste Special can selectively paste certain aspects of the data you are copying instead of the whole thing.
The common use of this command is to copy over just the formulas used, but you can also do the reverse and paste just the values instead. This gives you the results of the original calculations in a hard-coded form so that any changes to the original numbers will not affect them.
- First, select the cells you want to copy.
- Right-click and select the Copy option, or just hit Ctrl + C.
- Now right-click on your target cell to paste the contents. The new menus have various paste-special icons arranged horizontally. Mouse over them to find the option for Values and click on it.
- The values of the cells will be pasted in the new location. You can select the cell and check the formula tab to see that it is a hard-coded value.
Is Hard Coding Worth It in MS Excel?
Unless you know what you are doing, avoiding any form of hard coding is a good idea. Remembering to use relative or, even better, absolute cell references and keeping your formulas clean will save you a lot of headaches down the line.
That being said, a careful user can take advantage of some specific ways of hard coding to make certain calculations more concise and elegant. Things such as named constants or operations performed through Paste Special can introduce hard-coded values without making a mess of your spreadsheet.
Manual hard coding, on the other hand, should always be avoided unless you are entering base data to use further in spreadsheet calculations.