Nick's guide to Excel / VBA Interview Questions

Nick's picture

I have hosted hundreds of Excel / VBA interviews, and I can work out very quickly how good someone is.

The biggest blunder to make in an interview is to say something like: "Oh, that's easy, I can do that in 5 minutes with the compiler and help files"

Well, in my interview, you have no compiler and no help files, so don't ignore syntax, or rely too much on help files.

You wouldn't turn up to a French interview with a dictionary and translate every word now would you ?

; - >

Here are some Excel VBA interview questions I might ask you.. Add a comment if you think you have a good answer.

Nick's picture

1. Dates

Name as many Date functions as you can in both Excel and VBA.

Date function for VBA and Excel

Date, Date Value, day, days360, today,weekday,year

to add more: month, datedif,

to add more:
month, datedif, datediff

To add another

Do not forget:
networkdays

Nick's picture

2. Calculation

Name as many ways as you can to make Excel calculate something.

Name as many ways as you can to make Excel calculate something

Unless the answer isn't every function from absolute to year (is there a Z?), pivot tables, recorded Macros, VBA etc I'm not sure what the question is asking. Can you expand?

Nick's picture

Excel Calculation

ok, I'll start u off with some easy ones:

  1. Press F9
  2. Enter a formula in a cell
  3. Select a cell containing a formula, press F2 then ENTER
  4. Create a pivot table
  5. Refresh a pivot table

 ... there are tonnes... the question is really to see how deep your understanding of Excel is

For example... would you know that when you have the formula entry screen up, tabbing through the arguments will cause a calculation of the function to occur.

Would u know that double clicking on a column heading can cause several types of calculation to occur...?

etc...

 

Excel Calculation

6. shift + F9
7. F2 a cell then enter

Not well known

Application.Evaluate()
Worksheet.Evaluate()
will both evaluate a statement you pass in as if it were a formula

 Change value with Scroll bar

 Change value with Scroll bar or Spin button

Select a formula in the

Select a formula in the formula bar and press F9. this will calculate the formula but will not print it out in the cell

Nick's picture

3. VBA Range Selection Question

You have the following data:
Data

- A1 is always populated
- the rest of the data may change
- write a VBA subroutine that selects A1 and up to and including the last populated cell on the diagonal going downwards and to the right.

So in this case: it will select A1:D4
Data

Diagonal

Option Explicit
Sub Diagonal()
    Dim i As Long
    Do
        i = i + 1
    Loop Until Range("A1").Offset(i, i) = ""
    Cells(1, 1).Resize(i, i).Select
End Sub
 

Nick's picture

Diagonal

Excellent start, but that won't always work

Diagonal

My mistake,  I read it as a continuous diagonal.  So we start from the other end.

Option Explicit
Sub Diagonal()
    Dim i As Long
    With ActiveSheet.UsedRange
        i = Application.Min(.Rows.Count, .Columns.Count) + 1
    End With
    Do
        i = i - 1
    Loop Until Range("A1").Offset(i - 1, i - 1) <> ""
    Cells(1, 1).Resize(i, i).Select
End Sub
 

Nick's picture

Diagonal

closer, but still not bullet proof..

Diagonal

Sub Diagonal()

Dim iLast As Integer
iLast = 1
Do While Trim(Cells(iLast, iLast)) <> ""
iLast = iLast + 1
Loop
iLast = iLast - 1
Range(Cells(1, 1), Cells(iLast, iLast)).Select

End Sub

Nick's picture

Diagonal

this won't always work

Could you please give an

Could you please give an example when this won't work ? (this is assuming A1 always in not empty and "space" is not being an "entry"). May be I didn't understand the requirement correctly.

Nick's picture

diagonal

That'll give it away..

; - >

There are 2 cases that will break your code.

What about this one: Sub

What about this one:

Sub GetDiagonal()
Dim lRowCt As Long
Dim lColCt As Long
Dim lCt As Long
With ActiveSheet.UsedRange
lRowCt = .Rows.Count
lColCt = .Columns.Count
For lCt = Application.Max(lRowCt, lColCt) To 1 Step -1
If .Cells(lCt, lCt) <> "" Then
ActiveSheet.Range(ActiveSheet.Range("A1"), ActiveSheet.Cells(lCt, lCt)).Select
Exit For
End If
Next
End With
End Sub

Nick's picture

select diagonal

close, but it won't always work.. there's a small bug too, you'll kick yourself when you see it.

I see a couple of potential

I see a couple of potential problems with my previous entry:

- The used range has more rows than there are columns available
- The active sheet isn't a worksheet

To cater for this, I modified the code like this:

Sub GetDiagonal()
Dim lRowCt As Long
Dim lColCt As Long
Dim lCt As Long
If TypeName(ActiveSheet) = "Worksheet" Then
With ActiveSheet
lRowCt = Application.Min(.UsedRange.Rows.Count, .Columns.Count)
lColCt = .UsedRange.Columns.Count
For lCt = Application.Max(lRowCt, lColCt) To 1 Step -1
If .Cells(lCt, lCt) <> "" Then
.Range(.Range("A1"), .Cells(lCt, lCt)).Select
Exit For
End If
Next
End With
End If
End Sub

Another problem may be that a

Another problem may be that a cell evaluates to an error result, to avoid a runtime error the cod emust be modified further to:
Sub GetDiagonal()
Dim lRowCt As Long
Dim lColCt As Long
Dim lCt As Long
If TypeName(ActiveSheet) = "Worksheet" Then
With ActiveSheet
lRowCt = Application.Min(.UsedRange.Rows.Count, .Columns.Count)
lColCt = .UsedRange.Columns.Count
For lCt = Application.Max(lRowCt, lColCt) To 1 Step -1
If .Cells(lCt, lCt).Text <> "" Then
.Range(.Range("A1"), .Cells(lCt, lCt)).Select
Exit For
End If
Next
End With
End If
End Sub

Nick's picture

Congratulations

well done... u nailed it.

hi guys, thanks a lot for

hi guys, thanks a lot for this brain teasing assignement.
Nick, please can you have a look at my code fully based on the previous one.
i change MAX by MIN in this line:
For lCt = Application.Max(lRowCt, lColCt) To 1 Step -1

doing so i think that it might increase the execution time as we dont need to evaluate unnecessary cells.

Sub GetDiagonalBonero()
'code fully on previous.

Dim lRowCt As Long
Dim lColCt As Long
Dim lCt As Long
If TypeName(ActiveSheet) = "Worksheet" Then
With ActiveSheet
lRowCt = Application.Min(.UsedRange.Rows.Count, .Columns.Count)
lColCt = .UsedRange.Columns.Count

For lCt = Application.Min(lRowCt, lColCt) To 1 Step -1
If .Cells(lCt, lCt).Text <> "" Then
.Range(.Range("A1"), .Cells(lCt, lCt)).Select
Exit For
End If
Next
End With
End If
End Sub

My first try in VBA

 Sub SelDiag()

ActiveSheet.Range("a1").Select

Do Until Selection.Value = Empty

    Selection.Offset(1, 1).Select

    Loop

DiagLen = Selection.Column - 2

ActiveSheet.Range("a1", Range("a1").Offset(DiagLen, DiagLen)).Select

End Sub

Nick's picture

Excellent 1st try

...good effort.

Have a look at the other solution posted recently..

What most people omit are the following:

  • Column limitations
  • Handling errors returned by the cell formula
  • Work on all types of sheet

 

 

select diagonal range

Public Sub selectDiagonal()
Dim c As Range, rDiagonal As Range
Dim i As Long

Set c = ActiveSheet.[b2]

While Not c.Column = Sheet1.Columns.Count
If LenB(c.Value2) <> 0 Then Set rDiagonal = c
Set c = c.Offset(1, 1)
Wend

ActiveSheet.Range([a1], rDiagonal).Select
Set c = Nothing
Set rDiagonal = Nothing
End Sub

Nick's picture

4. Excel function

Write an Excel function that calculates the first day of the current month

The first day of the current month right now is: 01-Jan-2009

First day

=TEXT(WEEKDAY(TODAY()-DAY(TODAY()-1)),"dddd")

Nick's picture

First day of the month

that's the day of the week of the first day of the current month

First day

I seem to have problems with these questions!  What else should the answer display?

First Day

If I understand the question right - below is the number of days in current year starting from Jan 1 to the first day of this month (including)

=DAYS360(DATE(YEAR(NOW()),1,1),DATE(YEAR(NOW()),MONTH(NOW()),1))

Nick's picture

First Day

maybe I need to clarify

The first day of the current month right now is: 1st May 2009

The it will be only the part

The it will be only the part of the above:
=DATE(YEAR(NOW()),MONTH(NOW()),1)

Nick's picture

1st day

is correct... u cld also use TODAY instead of NOW.

1st day

you could also use =eomonth(today(),-1)+1

First Day

=TEXT(WEEKDAY(DATE(YEAR(NOW()),MONTH(NOW()),1)),"dddd")

First Day of current date

=TEXT(TODAY()-DAY(TODAY())+1,"ddd, mmm dd, yyyy")

1st day better command

= DateSerial(Year(Date), Month(Date), 1)

Nick's picture

DateSerial

DateSerial is a VBA function.. not Excel

ANSWER-First Day of current month

=TEXT(MONTH(NOW),"DDDD") Thanks

Correct One: First day of Current month...

=TEXT(WEEKDAY(TODAY()-DAY(TODAY()-1)),"dddd") for more help, just mail me urs problem.. Thanks

An even tougher question is last day of current month :)

 

=DATE(YEAR(TODAY()),MONTH(TODAY())+1,1)-1

 

first day of the current month

dt = Year(Now()) & "/" & Month(Now()) & "/" & "1"

Nick's picture

5. Excel Function

You have the following data: Data

Write a function to go in cell C3 that will return the house name that corresponds to the number typed in C2

Ans 5

=IF(B2="","", VLOOKUP(B2,E2:F8,2,0))

Nick's picture

Ans 5

Yep

Ans 5

Nope

Nick's picture

Ans 5

you're right... it's wrong... there's a small oversight in the references

VLOOKUP(C2,E2:F8,2,0)

VLOOKUP(C2,E2:F8,2,0)

It would say this was wrong...

i think its
VLOOKUP(C2,E3:F8,2,false)

that is E3 instead of E2

Solution

No need of using if function for this.

Formula is =VLOOKUP(C2,E2:H8,2,FALSE)

Thats it, it returs the value searching for is Building name

Nick's picture

6. VLOOKUP vs INDEX MATCH

What's the difference between using VLOOKUP vs INDEX + MATCH ?

Vlookup will allow you to

Vlookup will allow you to search for an item in the 1st column.
The other will allow the seach param on any column

Vlookup must always return a

Vlookup must always return a value to the right of the lookup column. Using the Index and Match functions together, you can return a value to the left of the lookup column.

Nick's picture

7. Volatile functions

What's a Volatile function ?

Functions that will trigger

Functions that will trigger recalculations every time a calculation is performed, e.g. F9.

Nick's picture

8. Volatile functions part 2

Name as many volatile functions as you can

They are

RAND
RANDBETWEEN
TODAY
NOW
ROW
COLUMN
 
 

Volatile functions part 2

OFFSET
INDIRECT
VLOOKUP
HLOOKUP

Nick's picture

9. Pivot Tables

Explain what a pivot table does

Pivot Tables enables us to

Pivot Tables enables us to summarize & analyze data in Lists & Tables. They can automatically sort, count & total the data stored in one table or spreedsheet & creates a second table displaying the summarized data.
The user sets up & changes the structure by dragging a dropping fields graphically.
Saurabh Singh

Hakim's picture

10. Missing and repeated number

You have a set of consecutive numbers from 1 to N, we remove a number at random and duplicate another number from the remaining, we place these numbers in an array and shuffle it. Write a VBA function when passed that unsorted array prints the missing and the duplicated numbers.

Hakim's picture

Here is a hint:  the sum of

Here is a hint:  the sum of squares of numbers from 1 to N is  N*(N+1)*(2*N+1)/6

 

Sub FindInArray(InputArray()

Sub FindInArray(InputArray() As Integer)
Dim N As Long
Dim lNSum As Long
Dim lNSqSum As Long
Dim lSum As Long
Dim lSqSum As Long
Dim lSumDiff As Long
Dim lSqSumDiff As Long
Dim iLess As Integer
Dim iMore As Integer
Dim strResult As String

N = WorksheetFunction.Count(InputArray())

lNSum = N * (N + 1) / 2
lNSqSum = N * (N + 1) * (2 * N + 1) / 6

lSum = WorksheetFunction.Sum(InputArray())
lSqSum = WorksheetFunction.SumSq(InputArray())

lSumDiff = lSum - lNSum
lSqSumDiff = lSqSum - lNSqSum

iLess = (lSqSumDiff - lSumDiff ^ 2) / (2 * lSumDiff)
iMore = (lSqSumDiff + lSumDiff ^ 2) / (2 * lSumDiff)

strResult = "Less Value = " & iLess & vbCrLf & "More Value = " & iMore

MsgBox strResult
Debug.Print strResult

End Sub

Define a public constant

Define a public constant -
Const N As Integer = 20

Add two buttons on the WorkSheet -
*-Jumble - Associate following code to it -
Public Sub Jumble()
For Each myCell In Worksheets("MyCustomSheet").Range("MyList")
Do
i = CInt(Rnd * N)
Loop While Cells(i + 2, 5) <> "" And i < N + 1
Cells(i + 2, 5) = myCell.Value
Next
End Sub

*-Find Issues - Associate following code to it -
Public Sub Findissues()
For i = 1 To N
flag = 1
For Each myCell In Worksheets("MyCustomSheet").Range("RandomList")
If myCell.Value = i And flag = 0 Then
flag = 2
End If
If myCell.Value = i And flag <> 2 Then
flag = 0
End If
Next
If flag = 1 Then
Cells(i, 10) = "Missing - " & i
End If

If flag = 2 Then
Cells(i, 10) = "Repeated - " & i
End If
Next
End Sub

Now in the Sheet itself define 1 to N numbers in consecutive cells, and define this as a named range - "MyList"

Now in 2 columns away.. define the entire column as a named range -"RandomList"

Now when you press the button Jumble you will get the list of 1st N numbers jumbled up in Named range "RandomList"

Now click on the Find issues.... it will populate the results in 10th column,... as to which elements are missing and which are repeated.