In April 2011, Mdsuhair asked a question at the Chandoo.org Forums
This is a candidate for Solver.
This post will walk us through the solution to the problem using Solver. It uses Solver and screen shots from Excel 2007.
All versions of Excel have solver available. Users should note that the screen shots may not match your version although the functionality will, some of the functions appear in different locations in different versions of Solver.
What and Where Is Solver
What is Solver
Solver is an Excel add-in that can solve problems by enabling a Target cell to achieve some goal.
This goal may be to minimse, maximise, or achieve some target value.
It solves the problem by adjusting a number of input cells according to a set of criteria or constraints which are defined by the user.
Where is Solver
Solver is an Excel add-in supplied with Excel, but not enabled by default.
To enable solver
File, Excel Options, Add-ins, Manage Excel Add-ins, Select the Solver Add-in checkbox
Solver will now appear as a New Tab on the Data, Analysis Tab
Solver Example
This post is based around a worked example Solver Example File, the file is compatible with all versions of Excel.
Defining the Problem
Mdsuhair had a series of 8 Items each which had a value:
He wants to know which items should be combined so that the sum of the values of the items is nearly equal.
To do this we need to assign each item into a Bucket. Namely Bucket A and Bucket B.
We will put values of 1 into a Bucket for each Item to show that it is assigned to that Bucket and a value of 0 to show that the bucket is empty (In/Out) Value.
We can start by adding a Total Column, which counts items in Buckets A + Bucket B for each Item, It then totals the totals.
We also know that we need to work out the Value of each Item in each Bucket
We do this by multiplying the Items Value by the Buckets (In/Out) value
Finally we need to add up the values in each Bucket and work out the difference between them
Mdsuhair’s original problem was to minimise the difference between the sum of the values in the 2 buckets.
Now we can place values of 1 in the buckets manually and Excel will show us the value of each Bucket and the Difference between them in Cell G11.
I have applied some conditional formatting to show when a cell has a value > 0.
The problem is that there are 28 or 256 combinations of answers, and to test them all manually at 1 every 5 seconds would take 21.3 minutes, assuming we can keep up that pace and remember which was the best combination.
This is where solver comes to the fore.
Applying Solver
To apply solver we need to define a series of requirements, rules and constraints.
These requirements, rules and constraints guide solver and set limits which allow solver to quickly narrow in on the answer.
What are our rules
Our main requirement is to minimise the difference between the value of the 2 buckets.
The difference between the 2 buckets in our example is cell G11, the sum of Bucket 2 values minus the Sum of Bucket 1 values.
We want to have G11 as low as possible but greater than or equal to 0.
We also know that an item can only be in Bucket A or Bucket B, it can’t be in both and can’t be Broken apart.
That is 2 Constraints for each Item
Firstly The Total column must be equal to 1
Secondly the Buckets Values must be Integers
We also know that the Total Number of Items is 8, this is another constraint.
We will discuss how these constraints are used in the next section
The Solver Window
This section will explain the solver window and its use in defining the problem within solver.
A Blank Solver Window
A Filled Solver Window
Set Target Cell:
This is the Target cell which is the cell which you are trying to solve the problem for.
Our Target cell is G11, The difference between the 2 Buckets values
Equal To:
The Equal To: section defines what we want to do with our Target Cell.
We want to achieve the same value in each Bucket and so the difference between the Buckets will be 0.
It might sound strange but we don’t want to minimise that difference. A minimal value will be achieved when all the Items are placed in Bucket A, as our equation for G11 will then have 0 – Total which is –Total, which is more minimal than 0.
Another way to constrain this is to Change G11 to =Abs(G10-H10)
This allows us to use Min as an Equal To: Value
But for now we can just leave G11 as =G10-H10 and we will set the Equal To: section as 0.
By Changing Cells:
Changing Cells: refers to the cells which will be modified by Solver to try and solve the problem.
We want to let Solver change the number of items in each bucket, this is the range: $C$2:$D$9
Hint: You can try the Guess button next to the Range Reference and Solver will take a Guess at what cells the problem is dependent on.
Always check this if you use it, especially in complex models.
Subject to the Constraints:
Constraints are the rules which define the limits of the possible solutions to the problem
We will add several constraints for our rules:
1. The Total column must be equal to 1 for each Item
2. The Bucket Value must be an Integer
3. The total contents of the 2 buckets must be 8 items
4. You could add a further constraint that each Bucket should hold the same number of items
Hint: As a general Rule, Under Constrain rather than over constrain! You can always add more constraints later.
To do this we will use the Add Constraint Button
1. We need to add a constraint for each cell in the Range E2:E9 that it is only allowed to be = 1
This constraint must be applied for each cell in the range E2:E9
2. We need to add a constraint for each cell in the Range C2:D9 that it is only allowed to be an integer
This constraint must be applied for each cell in the range C2:D9
3. We need to add a constraint for the Total of the 2 Buckets, E10=8
You can Change or Delete Constraints if you make a mistake by selecting the appropriate constraint and using the Change or Delete Buttons
Save and Load Solves Parameters
Selecting the Options Button there is the Option to Save Model and Load Models.
Hint: The Save/Load Models has been shifted onto the main Solver dialog in Excel 2010.
Using the Save Model and Load Model options you can Save and the Load the Solver Parameters for your model. The Save Model saves the parameters in a Range of cells as shown below.
This allows an easy way to actually setup and/or change the solver parameters.
Hint: Setup one constraint using solver then Save the model. Edit the model on the worksheet and re-load the model as required.
Note: That the parameters although when saved show as True/False or Numbers are all Excel Equations, see above.
This means you can edit them to change the Constraints and Parameters as required and re-load them into solver.
I have included 3 sets of Parameters for our model.
These are:
- Base Case – Forces bucket values to be equal, Allows uneven bucket counts
- Equal Sized Buckets – Forces each bucket to contain the same number of items
- Force an error – Which forces an error in the solver model
Load each model and try them at your leisure.
Running the Solver Model
Warning: Solver is a computationally complex add-in, so once your model is setup, Save your Workbook.
Prior to running the model there are a few parameters we should look at to ensure the model solves correctly.
On the main Solver window select the Options button. (Some of these parameters are on the Main Solver window in Solver 2010)
Generally you can accept the defaults but in this case we will change the following
Assume Linear Model – Select
Assume Non-Negative – Select
Note: Solver in Excel 2010 will return a better answer without these 2 parameters enabled by default
The other 2 parameters which you may need to change from time to time is
Precision: Precision is a number from 0 to 1 and higher means more precise
Tolerance: Tolerance shows how far away from a Number, an Integer constraint is allowed to be
The use of the Estimates, Derivatives and Search parameters are beyond the scope of this post. I direct you to the Excel Help on these subjects, by selecting the Help button.
Run the Model
To Run the Model, select the Solve Button from the main Solver menu.
The main status bar in Excel will flash up a number of statistics about the internal workings of the Solver add-in. Generally these flash by and are too fast to read. If a model is too complex it may stall and you won’t see any movement for a while. Solver generally recovers from these problems itself.
Once the Solver model finishes it will display a dialog of the results and allow you to do several things
First thing to note is that “Solver Found a Solution“.
If it has found a solution, the worksheet cells will be changed to show the solution
You now have 4 options:
- Run a Report
- Save a Scenario
- Return to the model
- Check Your Results
Run a Report
Run a report by clicking the report you want.
A new sheet will be added to your workbook depending on the report but will be called:
- Answer Report 1,
- Sensitivity Report 1
- Limits Reports 1
etc.
Note, that not all reports are relevant at all times, depending on the Constraints you have applied.
Save a Scenario
Selecting the Save a Scenario button takes you to the Save Scenario dialog.
Type in a Name and the Scenario of your model is saved as a Scenario.
Scenarios as available for use in the Scenario Manager, which is accessed from the Data, What-If-Analysis Tab
Return to the model
You can return to your model and either:
- Keep Solver Solution
- Restore Original Values
Check the Results
Solver is probably the most Black Box’ish of systems within Excel. As such any results it puts out must be manually checked for suitableness before further use.
These checks for realness, should as a start confirm that the results meet all the criteria supplied.
Are the results roughly what were expected?
Are any Minimums or Maximums violated?
What If Solver Doesn’t Find a Solution?
From time to time Solver will return with an error that a “Solver could not find a feasible solution.”
When this happens it is indicating one of several possibilities:
- Your model is over or under constrained
- Your model constraints are impossible to meet
- Your model constraints have an error
Start by checking the current constraints for errors and ambiguities
Eg: in our case we have 8 items so requiring the Count of the two buckets to be 20 is impossible to meet
My Solver Answer has Strange Numbers?
In solver up to and including Excel 2007, solver would commonly return numbers like 3.5E-18.
This is 0.0000000000000000035, which is effectively 0
If your model returns these, feel free to go through the model and change them to 0, in our model we should also check as the corresponding 1, may in fact be 0.9999999999999999965.
It should be noted that this problem in Solver in Excel 2010 does not occur as often but will still occur.
What and How have you used solver in the past?
What and How have you used solver in the past?
Let us know in the comments below:



































43 Responses to “Quickly convert numbers stored as text [tip]”
Additional tip,
Select column which contains text -> Data -> Data tools -> Text to columns -> Finish
Chandu
This one is particularly awesome if you have tens of thousands of data to convert to number. Otherwise it can take excel forever (minutes to half hour or longer) to process an error correction.
This is definitely the best option, and has the added benefit that you can use it to convert text to numbers, and numbers to text, depending on whether you choose General or Text before clicking Finish!
when you use this method it's worth making sure that there are no delimiters selected (just in case)
another method is to do a find & replace (I routinely use zero with zero, or . with .)
ALT+A+E>>enter>>enter will do it 🙂
OMG Thank you Chandu! I was struggling with this so much with my big datasheets and now I am so happy! so funny.
In my excel though, it is the same pattern but it is:
1) Choose the data tab
2) highight the column you want to change
3) choose "text to columns" in the ribbon
4) select fixed width
5) enter
6) no crashes!!
Often the text which you want as a number will have a decimal point, so Select column which contains text -> Data -> Data tools -> Text to columns ->Select apostrophe to add also as the delimiter -> Goto Advanced and add the decimal point. -> Finish, Voila. It works. You can then format as currency etc. Worked in Ver 2013. Seems that MS is degrading some important functions so as to get users to upgrade to 365.
Sir, how convert text to number in Power Query.
I had never thought about multiplying the numbers by 1 before. Great tip. For those who love macros, I found a very well written VBA macro by Ejaz Ahmed (StrugglingToExcel.com). This macro not only converts the numerical text to numbers but also formats dates and trims the values (getting rid of those nasty leading/trailing spaces). Plus you can apply this to multiple columns at the same time! I immediately added it to my QAT bar and use it almost daily with my data extracts. Check it out!
http://www.thespreadsheetguru.com/the-code-vault/2014/8/21/convert-numbers-stored-as-text
This tip is awesome! But one thing I run into constantly is the need to convert text to number and keep the leading zero, if there is one. I work a lot with SSNs and zip codes, etc. Any help, much appreciated!!
Hi Sue,
let ur zip code (length 5) in Column B, then select Column B and go to 'Format Cell' (CTRL + !) - Number - Custom - enter 00000 in Type field.
Now put, 15 in cell B1 and it will show 00015.
Hope this will solve ur query.
1) SSNs and ZIP codes are not numeric. They are meant to be character based identifiers. With numbers, leading zeros to the left of the decimal are not significant and are truncated. It may sound terribly picky of me to bring up the distinction, but I've learned that it does make a difference in some cases. (Especially when delivering to a client who is attempting to extract and then load your data into a different DBMS.)
2) The option described by SAURABH below (custom format, 00000) will work in Excel but it's only displaying the number as '00015' while the actual value of the cell will still be 15 because you have converted it to a number and excel will pay attention to significant digits (see above.) Meaning, if you "Paste special" with values only into a new cell, it will paste '15' into the cell rather than '00015', which could lead to problems depending upon how you need to carry them into new work. Your client's ETL process may bring in '15' rather than what you intend '00015.' I usually leave SSNs and ZIP as text, that way leading zeros (and dashes in the case of SSNs) are preserved.
Better than #2 (don't waste time dirtying and clearing a cell)... Copy a blank cell (really blank, not containing anything), select cells to change, Paste Special Values, Operation Add.
+1. Excellent tip.
An alternative to multiplying the numbers by 1 is to add 0 instead by using the same process as the multiplication method. At least you can save a step. You don't have to enter a 1 to multiply. The blank cell is 0 and can be added to change text to a number.
One thing I find very handy when doing this, as I often have intermittent numbers as text:
Select the first cell that has the warning flag, then ctrl-down arrow to the last one. Once you have it all, especially if it's thousands of cells, it's annoying to scroll back up to get to the flag. If you apply formatting, such as setting the cell fill color to none, it automatically takes you back up to the top without losing your selection.
Good Tips !
I knew both the tips before. I like the tip mentioned by "Jon Peltier". Amazing + Awesome 🙂 He steals the Show .. I mean this Post 🙂 😉
Thanks for contribution to all !
Regards,
Rahim Zulfiqar Ali
Love this post. I often have to export SAP Reports to excel and then do various sorts and lookups. This text issue has been driving me crazy. I especially like Jon Peltiers method where we can add a blank cell via paste special. Will be forwarding this tip to my friend Michael Martin.
yes, Michael Martin was impressed!
I also benefitted from Ctrl Alt V to paste special. I don't know how I have missed this one all these years. This is something we do a lot around here.
Hi again! One reason I asked about the leading zero is not so much for display purposes. I realize that '15' formatted to '00015' still has a value of '15'. That is one of my issues!! I receive data from multiple sources and need to constantly do look-ups and queries, and I drive myself crazy trying to format the different spreadsheets so they can 'talk' to each other. Does anyone have a tried and true process for syncing up columns where numbers are stored as text? For me, it's SSN, personel number, and zip. And, I think some of the sources they ARE number and some (like our DB queries) come in as text. aaargh. And, thank you in advance.
Ah, I see. I'd format anything ZIP or SSN related to text, and then clean up my lookup tables to be in that format as well. To convert those '15's back to '00015's there are two ways that I use.
1) If I'm reformatting an entire workbook, I actually DO use the custom format to adjust some columns, like zip codes or dates in an unusual format (like '21 Aug 14', etc.) when I have everything like it needs to be, I save the result as a .CSV file. When excel saves a custom format as .CSV, it defaults to value (the display option) and discards value2 (the underlying actual value.) Text files have no record of formats, they are lean by their nature, so when you open it, Excel will attempt to interpret each column. Because of this, I close the CSV, and I use the text import wizard, (Data Tab > "From Text") to bring in the CSV. The import wizard lets me specify which columns I want to be Characters and which I want to be numbers, (and it does dates as well for good measure.)
2) If I'm only able to reformat a single column, I'll usually do that via a Macro, for example, if the zip codes have had their zeros truncated by excel, I format that column as text, and run this "padding" macro that I wrote for that purpose:
Sub Pad_to_X()
On Error Resume Next
'This will insert zeros in front of a number.
'X is the length of the entire number plus zeros
'so if you have 1 and want 001, X would be 3
With Application
.DisplayAlerts = False
End With
x = InputBox("Enter X, and the selection will be padded with leading zeros to X characters")
For Each cell In Selection
lenc = Len(cell)
diff = x - lenc
If diff > 0 Then
padme = Empty
For nn = 1 To diff
padme = padme & "0"
Next nn
cell.Value = padme & cell.Value
End If
Next cell
With Application
.DisplayAlerts = True
End With
End Sub
Cleaning data from multiple sources is fun. Often a lookup will bomb because the table or the dataset has characters that mean something to the client's software (or webpage,) but are "invisible" when you look at them in excel. In those cases, I recommend trimming leading and trailing whitespace, and looking for and removing chr(160) (HTML Non-Breaking whitespace.) Those are the most common.
Sometimes the character in a cell is something you've never considered, and in the cases where a value isn't found in a lookup table, I'll run this "decode string" macro to split out the cell value and display it by its ASCII equivalent. It'll identify any weird relics, which you can then sweep for with a cleaning macro:
Sub Decode_String()
Dim sttrarray(1 To 5000) As Variant
Dim sttrarray2(1 To 5000) As Variant
'instring = InputBox("String to Decode?")
instring = Selection.Value
lenstring = Len(instring)
count = 0
For x = 1 To lenstring
sttrarray(x) = Asc(Mid(instring, x, 1))
sttrarray2(x) = Mid(instring, x, 1)
count = count + 1
Next x
Workbooks.Add
targ = ActiveWorkbook.Name
sht = ActiveSheet.Name
Workbooks(targ).Sheets(sht).Range("A1").Value = "Position"
Workbooks(targ).Sheets(sht).Range("B1").Value = "Character"
Workbooks(targ).Sheets(sht).Range("C1").Value = "ASCII decode"
For n = 1 To count '
Outp = Outp & "Position " & n & " is " & sttrarray2(n) & " or chr " & sttrarray(n) & Chr(13)
Workbooks(targ).Sheets(sht).Range("A" & n + 1).Value = n
Workbooks(targ).Sheets(sht).Range("B" & n + 1).Value = sttrarray2(n)
Workbooks(targ).Sheets(sht).Range("C" & n + 1).Value = sttrarray(n)
Next n
End Sub
Multiply 1, divided by 1, add or subtract 0 all do the trcik... ;p
Select the column and hit ALT+D+E and Finish till dialogue box disappears. We are good to go
Instead of typing 1 and copying it, just copy any blank cell and go to paste special and add.
It happens that I need to attach some data from external source over and over again and the data comes in text format. If these procedures are too difficult, I use extra column to have values in numbers. The formula is very simple: =value()
You go from "sometimes text-numbers may be scattered across the worksheet, making selection of cells a pain." to "3. Select all the cells that have text-numbers."
You just said it's a pain to select the cells... so you instruct us to do it anyway? Am I the only one who fails to see logic here?
@Belgianbrain:
You dont have to individually select such cells. you can select entire range that contains such data and do it. That is what I mean by "Select all the cells"
Good Tips! Thanks for sharing, very useful when SAP reports must be exported
Put zero in a cell >> Copy >> Paste Special >> Add
...will also do that.
Awasome... Chandooo..
But if data is too large u can use the Function =Value( & Then Use Paste Special.
Else this trick is superb.
Love this website!!!!
The "enter 0", copy - paste special - add method puts a zero in blank cells.
Similarly, for the enter 1- copy paste special multiply.
The copy blank - copy - paste special - add method does not seem to have this drawback.
Thank you.
If you do use the first option (click on 'Convert to number') and you're working in a large model, make sure you turn on the Manual calculation mode. Otherwise Excel will recalculate after each converted number. This can be really annoying if a model takes 0.1 seconds to recalculate and you just told Excel to convert a couple of thousand numbers!
The Value() formula works fine as well, which is good for external data, or to be used in LOOKUP functions. The other way around, by the way, if you must LOOKUP a value and you're looking it up in an array of text values, I use =TEXT(A1,"0") to convert a value to its string equivalent.
I also love the Add 0 tip, I hadn't thought of that!
[…] If you import data that has numbers formatted as text, Chandoo shares a quick tip for fixing them. […]
Really a useful tip,for me my users use SAP as input where often tedious to select all the cells and again go to the top row to convert numbsers.
Really a very useful tip.
To those keyboard people:
To access this "error handling menu", press the Alt + Right Click Button, then press the "C" to convert to number... this last "C" is in portuguese, I dunno what the equivalent in english is.
Also another thing I learned together with this trick. If you are using one of these new keyboard, that doesn't have a Right Click button, the equivalent to it is Shift + F10... so it would be Alt + Shift + F10 then "C"
This tip (Tip#2: Paste Special Convert) saved my life! I've always done all my conversions the 1st way. Working across multiple sheets exported from my Accounting program, all the zeros are always listed as text. It was so frustrating - and then this tip came along! Thank you thank you thank you! I now just select all sheets, do the ctr+alt+v thing and voila! all text is now zeros!
Very very useful tip 🙂
Chandoo Tu Bhai hai
Thanks a lot
Hi All,
I have few data with month as column name and Planned hours, forecast for months,actuals hours , ETC as Row data. What I want is whenever the user enter values in forecast month(Current month) , it shld color the cells.and when the user enter the values in Actuals hours(it will be of prevous month) it shld fill the color.The cycle will continue . Also As the month will pass on the previous valued cells shld be in no format.
https://www.youtube.com/watch?v=IbVwhrfegcI
My excel crashes to the point it is unusable. The only method I have found that wont crash my machine is find & replace (replace 1 with 1, and so on.) I am not sure what causes this in my sheets, and it can even cause another machine to start having this issue. I assume it is settings or the data itself. I created a dump file off the process before crashing, which ended up being huge. Looking in the file with notepad, some data I can't see. But towards the bottom of the file there is a ton of words - waiting on word wrap to finish in it to see what it says.
@aly
a few things
What type of PC is it?
How much RAM?
What version of Windows/Excel are you using ?
Do you know how many lines of data you have ?
Can you ask the question in the Chandoo.org Forums?
https://chandoo.org/forum/
Please attach the data file so we can give you more specific help