Excel interview questions
13 questions with answers. 3,913 open jobs in India ask for Excel.
Is there Infinity in Spreadsheets?
I like to use 1e99 as it gives the largest number with the fewest keystrokes but I believe the absolute maximum is actually 9.99999E+307. At that stage of the number spectrum I don't think there is much difference as far as Excel is concerned.
From Stack Overflow: asked by k88074, answered by user4039065 (CC BY-SA 3.0)
How can I create a string or formula containing double quotes in Excel?
Try escaping with an additional double quote. By escaping a character, you are telling Excel to treat the " character as literal text.
= "Maurice ""The Rocket"" Richard"From Stack Overflow: asked by Allain Lalonde, answered by YonahW (CC BY-SA 4.0)
Shortcut to Apply a Formula to an Entire Column in Excel
Try double-clicking on the bottom right hand corner of the cell (ie on the box that you would otherwise drag).
From Stack Overflow: asked by John Shedletsky, answered by soo (CC BY-SA 2.5)
Check whether a cell contains a substring
Try using this:
=ISNUMBER(SEARCH("Some Text", A3))This will return
TRUEif cellA3containsSome Text.From Stack Overflow: asked by geotheory, answered by gwin003 (CC BY-SA 3.0)
Return empty cell from formula in Excel
You're going to have to use
VBA, then. You'll iterate over the cells in your range, test the condition, and delete the contents if they match.Something like:
For Each cell in SomeRange If (cell.value = SomeTest) Then cell.ClearContents NextFrom Stack Overflow: asked by Bryan Ward, answered by J.T. Grimes (CC BY-SA 3.0)
Excel: last character/string match in a string
I think I get what you mean. Let's say for example you want the right-most
\in the following string (which is stored in cellA1):Drive:\Folder\SubFolder\Filename.extTo get the position of the last
\, you would use this formula:=FIND("@",SUBSTITUTE(A1,"\","@",(LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))/LEN("\")))That tells us the right-most
\is at character24. It does this by looking for"@"and substituting the very last"\"with an"@". It determines the last one by using:(len(string)-len(substitute(string, substring, "")))/len(substring)In this scenario, the substring is simply
"\"which has a length of1, so you could leave off the division at the end and just use:=FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))Now we can use that to get the folder path:
=LEFT(A1,FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))Here's the folder path without the trailing
\:=LEFT(A1,FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))-1)And to get just the filename:
=MID(A1,FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))However, if you can assure your string in
A1does not contain repeated spaces between words†, here is an alternate version of getting everything to the right of the last instance of a specific character. So using our same example, this would also return the file name:=TRIM(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",LEN(A1))),LEN(A1)))† because this is what the
TRIMdocumentation states (emphasis mine):Removes all spaces from text except for single spaces between words. Use TRIM on text that you have received from another application that may have irregular spacing.
Since filenames can potentially contain repeating spaces, using the
TRIMbased solution can give incorrect results with repeating spaces replaced by single spaces.From Stack Overflow: asked by geotheory, answered by tigeravatar (CC BY-SA 4.0)
How to keep one variable constant with other one changing with row in excel?
Use this form:
=(B0+4)/$A$0The
$tells excel not to adjust that address while pasting the formula into new cells.Since you are dragging across rows, you really only need to freeze the row part:
=(B0+4)/A$0Keyboard Shortcuts
Commenters helpfully pointed out that you can toggle relative addressing for a formula in the currently selected cells with these keyboard shortcuts:
- Windows: f4
- Mac: CommandT
From Stack Overflow: asked by dude, answered by Alex Brown (CC BY-SA 3.0)
Find last used cell in Excel VBA
NOTE: I intend to make this a "one stop post" where you can use the
Correctway to find the last row. This will also cover the best practices to follow when finding the last row. And hence I will keep on updating it whenever I come across a new scenario/information.Unreliable ways of finding the last row
Some of the most common ways of finding last row which are highly unreliable and hence should never be used.
- UsedRange
- xlDown
- CountA
UsedRangeshould NEVER be used to find the last cell which has data. It is highly unreliable. Try this experiment.Type something in cell
A5. Now when you calculate the last row with any of the methods given below, it will give you 5. Now color the cellA10red. If you now use the any of the below code, you will still get 5. If you useUsedrange.Rows.Countwhat do you get? It won't be 5.Here is a scenario to show how
UsedRangeworks.xlDownis equally unreliable.Consider this code
lastrow = Range("A1").End(xlDown).RowWhat would happen if there was only one cell (
A1) which had data? You will end up reaching the last row in the worksheet! It's like selecting cellA1and then pressing End key and then pressing Down Arrow key. This will also give you unreliable results if there are blank cells in a range.CountAis also unreliable because it will give you incorrect result if there are blank cells in between.And hence one should avoid the use of
UsedRange,xlDownandCountAto find the last cell.Find Last Row in a Column
To find the last Row in Col E use this
With Sheets("Sheet1") LastRow = .Range("E" & .Rows.Count).End(xlUp).Row End WithIf you notice that we have a
.beforeRows.Count. We often chose to ignore that. See THIS question on the possible error that you may get. I always advise using.beforeRows.CountandColumns.Count. That question is a classic scenario where the code will fail because theRows.Countreturns65536for Excel 2003 and earlier and1048576for Excel 2007 and later. SimilarlyColumns.Countreturns256and16384, respectively.The above fact that Excel 2007+ has
1048576rows also emphasizes on the fact that we should always declare the variable which will hold the row value asLonginstead ofIntegerelse you will get anOverflowerror.Note that this approach will skip any hidden rows. Looking back at my screenshot above for column A, if row 8 were hidden, this approach would return
5instead of8.Find Last Row in a Sheet
To find the
Effectivelast row in the sheet, use this. Notice the use ofApplication.WorksheetFunction.CountA(.Cells). This is required because if there are no cells with data in the worksheet then.Findwill give youRun Time Error 91: Object Variable or With block variable not setWith Sheets("Sheet1") If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastrow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else lastrow = 1 End If End WithFind Last Row in a Table (ListObject)
The same principles apply, for example to get the last row in the third column of a table:
Read the rest at the sourceFrom Stack Overflow: asked by Mushahid Hussain, answered by Siddharth Rout (CC BY-SA 4.0)
How can I perform a reverse string search in Excel without using VBA?
This one is tested and does work (based on Brad's original post):
=RIGHT(A1,LEN(A1)-FIND("|",SUBSTITUTE(A1," ","|", LEN(A1)-LEN(SUBSTITUTE(A1," ","")))))If your original strings could contain a pipe "|" character, then replace both in the above with some other character that won't appear in your source. (I suspect Brad's original was broken because an unprintable character was removed in the translation).
Bonus: How it works (from right to left):
LEN(A1)-LEN(SUBSTITUTE(A1," ",""))– Count of spaces in the original string
SUBSTITUTE(A1," ","|", ... )– Replaces just the final space with a|
FIND("|", ... )– Finds the absolute position of that replaced|(that was the final space)
Right(A1,LEN(A1) - ... ))– Returns all characters after that|EDIT: to account for the case where the source text contains no spaces, add the following to the beginning of the formula:
=IF(ISERROR(FIND(" ",A1)),A1, ... )making the entire formula now:
=IF(ISERROR(FIND(" ",A1)),A1, RIGHT(A1,LEN(A1) - FIND("|", SUBSTITUTE(A1," ","|",LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))))Or you can use the
=IF(COUNTIF(A1,"* *")syntax of the other version.When the original string might contain a space at the last position add a trim function while counting all the spaces: Making the function the following:
=IF(ISERROR(FIND(" ",B2)),B2, RIGHT(B2,LEN(B2) - FIND("|", SUBSTITUTE(B2," ","|",LEN(TRIM(B2))-LEN(SUBSTITUTE(B2," ",""))))))From Stack Overflow: asked by James Eichele, answered by BradC (CC BY-SA 3.0)
Simple Pivot Table to Count Unique Values
UPDATE: You can do this now automatically with Excel 2013. I've created this as a new answer because my previous answer actually solves a slightly different problem.
If you have that version, then select your data to create a pivot table, and when you create your table, make sure the option 'Add this data to the Data Model' tickbox is check (see below).
Then, when your pivot table opens, create your rows, columns and values normally. Then click the field you want to calculate the distinct count of and edit the Field Value Settings:
Finally, scroll down to the very last option and choose 'Distinct Count.'
This should update your pivot table values to show the data you're looking for.
From Stack Overflow: asked by user1586422, answered by Meaghan Fitzgerald (CC BY-SA 3.0)
Remove leading or trailing spaces in an entire column of data
If you would like to use a formula, the
TRIMfunction will do exactly what you're looking for:+----+------------+---------------------+ | | A | B | +----+------------+---------------------+ | 1 | =TRIM(B1) | value to trim here | +----+------------+---------------------+So to do the whole column...
1) Insert a column
2) InsertTRIMfunction pointed at cell you are trying to correct.
3) Copy formula down the page
4) Copy inserted column
5) Paste as "Values"Should be good to go from there...
From Stack Overflow: asked by venkat, answered by hydrox467 (CC BY-SA 3.0)
Excel Date to String conversion
=TEXT(A1,"DD/MM/YYYY hh:mm:ss")(24 hour time)
=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")(standard time)
From Stack Overflow: asked by Chaitanya MSV, answered by Kirill (CC BY-SA 3.0)
Excel formula to reference 'CELL TO THE LEFT'
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),0,-1)From Stack Overflow: asked by mik, answered by Jason Young (CC BY-SA 2.5)
Related topics
Where these questions come from
Open-source collections on GitHub, official documentation, a government careers service and the Stack Exchange sites, each used under its own licence. The answers are the authors' own words: we only tidy the layout, leave out pictures, and cut a long answer with a link to the rest. Stack Exchange answers stay under CC BY-SA and name who asked and who answered.
- Stack Overflowby its contributors, named on each answerCC BY-SA13 questions