This blog post uncovers 6 different ways you can find the last row. In these examples the number of the last row is assigned to a variable for use within your procedures.

Are you new to Excel VBA? Learn Excel VBA quickly with this online course.

For these examples the following variables have been declared and the *sht* variable activated.

Dim sht As Worksheet Dim LastRow As Long Set sht = ThisWorkbook.Worksheets("Sheet1")

You may know that using the keyboard shortcut of **Ctrl + the down arrow **key will shoot you to the bottom of a list. The VBA code for the same shortcut is shown below.

This assumes A1 is the first cell of data.

LastRow = Range("A1").End(xlDown).Row

This is a common approach but has its disadvantages. This shortcut finds the bottom of a block of cells. This means that if you have blank cells in a range it will not accurately find the last row.

If you have blanks cells in a range, then you could work around that by starting quite far down a spreadsheet and using the shortcut to go up and hit the bottom (instead of top and down).

LastRow = Range("A100000").End(xlUp).Row

You will need to ensure the starting cell is further than you will ever need. In this example it is row 100,000.

The *UsedRange* property in Excel VBA is the range of filled cells on a spreadsheet.

It is extremely useful that Excel knows this, and we can use this information on a worksheet by using the shortcuts **Ctrl + End** and **Ctrl + Home**.

However this *UsedRange* needs resetting or it may contain outdated information about the sheet. The code below clears the *UsedRange* and then uses it to located the last row number.

sht.UsedRange.Clear LastRow = sht.UsedRange.Rows.Count

The CountA function can be used to count all non-blank cells in a range to find the last row.

Now like the first example, you will need to be careful of blank cells in a range causing a problem.

However in my blog post on creating a macro to remove blank rows from a list, I used this technique for exactly that reason. To find out how big the list is, when the blanks are removed.

LastRow = Application.CountA(Range("A:A"))

If you have set up a named range for your list, this can make it easy to find the last row. In the code below the named range of the list is *Sales*.

LastRow = sht.Range("Sales").Rows.Count

If you have already formatted your range as a table. We can used the table to locate the last row number.

LastRow = sht.ListObjects("Table1").Range.Rows.Count

- Loop Through all the Files in a Folder using VBA
- Count Cells in a Range By their Cell Colour
- Export Multiple Excel Sheets to a Single PDF
- Format Specific Words in a Cell using VBA

blah blah

]]>

This tutorial will show you how to display any symbol though, so you could insert a smiley face, hour glass, aeroplane and much more.

The first thing we need to do is find out the letter or number for the symbol we want to insert. Every symbol from the *Wingdings* libraries has an associated letter or number when displayed in a normal written font such as Calibri or Arial.

To find this out; start by inserting the symbol in a cell on your worksheet. Then select that cell and change the font to Calibri, Arial or some other written font.

The letter or number will now be displayed instead. For example, the smiley face symbol is J, and the hourglass is 6. Importantly for this tutorial, we know the thumbs up symbol is C, and thumbs down is D.

The following formula was written in cell D2 of the worksheet shown above.

=IF(C2>=B2,"C","D")

It tests if the value in C2 (this months sales) is greater than or equal to the value in B2 (last months sales). If it is then display C (remember this is the thumbs up symbol), and if not display D (thumbs down symbol).

Now that we have the IF function showing the correct value, all the cells containing the formula need to be formatted in a font such as Calibri to show the symbol.

In the example shown above, the thumbs up symbol is formatted in green, and the thumbs down symbol in red. This is done using Conditional Formatting.

- Select the range of cells containing the symbols.
- Click the
**Home**tab and then**Conditional Formatting**. - Select
**Highlight Cells Rules**and then**Equal To**. - Enter C in the first box and then select a green font colour from the
**Custom Format**options in the second box. Click**Ok**.

- Repeat these steps to display in a red font if the value is equal to D.

Displaying a different symbol based on the values of your worksheet, and applying automatic custom colours can add a creative wow factor to your spreadsheets.

Depending on the kind of data you work with, there are loads of symbols on offer and they may just have your spreadsheets jumping out of the screen.

The spreadsheet below shows a list of names with the answer in cell D2. *Ross* is the name that occurs the least in that list.

This formula returns the least frequent value from the list in A2:A16. The formula is explained below so keep reading.

{=INDEX(A2:A16,MATCH(MIN(COUNTIF(A2:A16,A2:A16)),COUNTIF(A2:A16,A2:A16),0))}

This formula is an array formula so you need to press **Ctrl + Shift + Enter**, and not **Enter**. This will put the curly braces around the formula. You do not type these.

Within this formula the COUNTIF functions are used to return how many times each name occurs in the list. The COUNTIF functions return the result below;

{2;5;5;4;4;5;4;4;5;2;4;5;4;4;4}

This means that the name in the first cell of that range (A2) occurs twice, 2nd cell (A3) occurs five times, 3rd cell (A4) occurs five times and so on.

The MIN function returns the smallest number from that array, which is 2 in this example.

The MATCH function is then used to search for the position of the first instance of 2 (the least mentioned names position). The result of this is 1, because the first instance of 2 is in the first cell of range A2:A16.

The INDEX function then returns the value which is in that cell (A2). Which in this example is *Ross*. Watch the video below for a visual explanation of this formula.

The INDEX and MATCH functions are awesome when used together for a flexible lookup formula. Find out more at this INDEX and MATCH tutorial.

Seriously improve your Excel Formula skills with our online course. Over 100 formulas covered. Sign Up Now.

]]>There is no real standalone function in Excel to do this, but it can be done. This could be a useful formula to find how many payments, or how many meetings until an end date.

The formula below calculates the number of Fridays between the date in cell A2 and the date in cell B2. The formula is explained below.

=B2-A2-NETWORKDAYS.INTL(A2,B2,16)+1

In this formula the start date is subtracted from the end date to begin with. This will leave us with how many days in total between the two dates.

In the second half of the formula, the NETWORKDAYS.INTL function is used to calculate the difference between the two dates excluding Fridays. This is then subtracted from the current total to leave us with how many Fridays there are.

The 16 in the function specifies to exclude the Fridays. When typing the function a list appears asking which days to exclude.

The +1 is added to the end because the NETWORKDAYS.INTL function calculates whole workdays. For example, you may consider the difference in days between today and tomorrow to be 1 day. NETWORKDAYS.INTL would return that answer as 2 as it uses each day as a whole day. So the +1 is added to counteract that.

I wanted to explain the other technique first as it probably makes more logical sense. However this tip was brought to me by one of my YouTube subscribers and seems to work.

I don’t really know how it works to be honest, but it does.

The NETWORKDAYS.INTL function is used, but in the third argument a string of 1’s and 0’s are used to specify what weekday you want to count. The example below counts the number of Fridays.

The string starts from a Monday. So this can be easily customised to your own requirements.

=NETWORKDAYS.INTL(A2,B2,"1111011")

- Five Awesome Date Functions in Excel
- Timesheet with Different Rates for Shift Work
- How Many Months Between Two Dates

]]>

Well unfortunately Excel does not yet have a Conditional Formatting feature that we can apply to charts. I have great news though. There is a way that we can do it, and it is not difficult.

This blog post will look at two examples of Conditional Formatting with charts so that you get a feel for how to do it. You can then apply the same technique to whatever example you need.

In the first example shown in the image below. We want to be able to highlight the column in each chart that corresponds to the product type selected by the user in cell B3.

To achieve this we first need to add a column to the table which will show the value if the product type is selected by the user, but show the #N/A error if it is not.

The IF function is used to perform this logical test and required actions. The NA() function is used to return the #N/A error if it is not the product type selected. This is done because the chart will not plot error values, so it essentially hides those values on the chart.

The formula below is the one entered into cell C4. It compares the product name against the one selected by the user in B3 of the *Sales By Product* worksheet. It then displays the value in cell B4 if it is a match, or shows the error value if not.

=IF(A4='Sales By Product'!$B$3,B4,NA())

In this example, Music & Sound has been selected by the user.

This data range can then be selected to create a chart. In this example we create a column chart.

Select range A4:C10 and click **Insert** > **Column Chart** and select the 2-D Clustered Column. It should look like below.

The error values are hidden and only the column for the selected product type is displayed.

Double click on the conditionally highlighted column (the orange Music & Sound) and edit the **Series Overlap** to say **100%**.

And our work is done. Continue to apply any further formatting that you think improves the chart.

In this example, we look at highlighting only the columns where the value represents and increase from last year, or last weeks data.

As you saw in the previous example, it is all about the IF function. We use a logical function like IF to perform the conditional test and only show the value if relevant.

Once we have this, we repeat the same chart technique as before to overlap the data series.

The formula below has been entered into cell C4. It compares the sale of this year (B4) against last years sales of the same product. Because the lists are in order largest to smallest by sales value, a VLOOKUP function is used to return the correct sales value.

=IF(B4>=VLOOKUP(A4,$E$4:$F$10,2,FALSE),B4,NA())

The end result looks like below showing only two products have increased sales since last year, or last week.

With this amazing technique you can apply any Conditional Formatting to charts in Excel. All the power comes from the logical function in the table.

Other typical examples might include highlighting actuals against target values, or the max and min values of a chart.

Two Essential Conditional Formatting Tricks You Need to Know

]]>We can find out how many cells by filtering the list by colour, and maybe use the AGGREGATE function to return the count. This is an option. But having to filter the list each time may not be good enough.

Because functions such as COUNTIF cannot count by cell colour, we will need to create our own custom function (also known as User Defined Functions or UDF’s) to get the job done.

- Open the Visual Basic Editor by pressing
**Alt +F11**or by clicking the**Visual Basic**button on the**Developer**tab. - Insert a new module if necessary by clicking the
**Insert**menu and then**Module**. - Copy and paste the code below into the code window (you can put this into the current workbook or the Personal Macro Workbook for global use).

Function COUNTIFCOLOUR(Colour As Range, rng As Range) As Long Dim NoCells As Long Dim CellColour As Long Dim rngCell As Range CellColour = Colour.Interior.Color For Each rngCell In rng If rngCell.Interior.Color = CellColour Then NoCells = NoCells + 1 End If Next COUNTIFCOLOUR = NoCells End Function

You can then use this function like any other function in Excel. The arguments do not appear like normal functions, but everything else is the same.

The user will select the range of cells to use and this is assigned to *rng*.

A For Next loop is then used to loop through each cell of that range checking if it is the colour we are looking for. If so 1 is added to the *NoCells* variable. This is assigned to the function for returning when the loop finishes.

Want to learn more Excel VBA? Check out the complete Excel VBA online course. You will see more examples of User Defined Functions and a whole lot more.

Whatever the timeframe being reported, this can mean a lot of time editing chart sources and formulas to show the right data.

This blog post looks at creating a dynamic rolling chart to show the last 6 months of data, so when new data is added to the table, the chart automatically updates to report the last 6 rows (months).

To create a rolling chart, we will first create two dynamic named ranges. These will automatically capture the last 6 months data. One named range for the chart data, and the other for its labels. We will then use these named names for our chart source.

The OFFSET function will be used to make the named ranges dynamic. This function enables you to reference ranges, relative to another range of a sheet. So this can be used to capture the last 6 rows.

Let’s first create the chart data named range.

- Click the
**Formulas**tab of the Ribbon and then the**Define Name**button. - Enter a name for the defined name. You can use whatever name you wish. I have called this one
*ChartData*. - In the
**Refers to**area, enter the formula below. The different parts of this formula can be edited to meet your needs.

This formula starts from cell B1. It then moves to the bottom of the column. The column bottom is found by counting how many values in column B. The -6 and 1 on the end of the formula are used to return the last 6 rows and 1 column from that cell.

=OFFSET(Sheet1!$B$1,COUNT(Sheet1!$B:$B),0,-6,1)

We now need to create another dynamic named range for the chart labels. The OFFSET function below will be used.

This function uses the previous defined name and selects a range of equal height that is one column to the left. In this example, that is column A.

=OFFSET(ChartData,0,-1)

The chart new needs to be edited to use the named ranges for its data and its labels.

- Click the
**Design**tab under**Chart Tools**on the Ribbon. - Click the
**Select Data**button.

- Click the
**Edit**button from the Series section on the left. - Cell B1 has been selected for the
**Series name**. This uses the Sales header for the name of the data series (in this example that is kind of redundant as I only have one series, but you may have more). - For
**Series Values**, the*ChartData*named range has been entered. Be sure to keep the sheet name in there too like in the image below.

- Click
**Ok**, and then click the**Edit**button for the Labels section on the right. - In the
**Axis label range**area, the*ChartLabels*named range has been entered. Once again be sure to keep the sheet name in the reference. And click**Ok**.

The dynamic rolling chart is created. If you add more rows to the table, the chart automatically updates to show the last 6 months.

Highlight the max and min values of a column chart

Create a scrollable chart for your Excel dashboards

]]>For the formula we will be using the CELL, MID and FIND functions. Let’s begin by looking at the CELL function.

The CELL function is a fantastic, and relatively unknown, function in Excel. Its purpose is to return information about a cell such as its column, cell colour, whether it is in a protected state or the filename.We need to use the function to return the filename of a cell. Because our goal is to return the sheet name, it does not matter what cell we use. Any cell on the sheet will work. In the example below, cell B1 has been used.

=CELL("filename",B1)

The function above will return the full filename of the cell such as;

*C:\Users\Trainee1\Desktop\[return-sheet-name.xlsx]London*

Now that we have the filename, we need to extract just the sheet name. The MID and FIND functions will be used for this.

The FIND function is used to return the position of the closing “]” + 1. The “]” indicates the end of the workbook reference, so the following characters are those for the sheet name.

The MID function then extracts up to the next 32 characters. This is an excessive number but because sheet names cannot have more than 31 characters it ensures the full name is returned whatever it may be.

Here is the full Excel formula to display the sheet name in a cell;

=MID(CELL("filename",B1),FIND("]",CELL("filename",B1))+1,32)

The group league tables are ranked as the results are entered. As soon as a groups fixtures have been completed, the knockout schedule is created. The quarters, semi-final and final fixtures are then calculated as these results are entered.

The spreadsheet is unlocked so you are free to check out the formulas and how it all works. Download the Euro 2016 planner spreadsheet.

There are many formulas and techniques used to create this Euro 2016 planner. They include;

**The SUMPRODUCT function** – This has been used heavily to perform the calculations at the group stages. I thought it was the best function to handle the calculation of a team’s games played, number of wins, goals scored etc. Learn more about the SUMPRODUCT function.

**Ranking and then calculating unique ranking** – The rankings for the group stages are done on the *Calculations* sheet (you will need to unhide it). On here I needed to calculate a teams ranking based on points, then goal difference and then goals scored. If a ranking was still tied, a unique ranking was formulated.

**Nested IF’s** – These have been used in the knockout stages to organise the fixtures as the results are entered.

**The VLOOKUP function** – It was guaranteed to be used somewhere. It has been used on the *League Tables* sheet (another hidden sheet) to formulate the group tables from the workings on the *Calculations* sheet. It is also used to help calculate the third place teams permutations (explained later in this post). Learn more about VLOOKUP.

**The Excel Camera tool** – A brilliant little tool. Used to create a dynamic picture of the group tables from the *League Tables* sheet onto the *Group Stages* sheet. Find out more about the Excel Camera tool.

These skills and techniques are all shown in detail in my online course for creating your own sports league tables and tournaments in Excel. Join hundreds of others in learning these skills. I will be on hand to help with any queries.

The UEFA European Championships 2016 has a new format for some of the fixtures in the last 16 (the first knockout round). The top 2 teams in each of the 6 groups are automatically through. They are then followed by the 4 best third place finishers.

A ranking is given to each of the teams who finish third in their group. The four best teams’ progress. Who they play in the next round is determined by the table below.

For example, the table above shows that if the 4 best third place teams are from groups A, B, C and D, then the winner of group A would play the third best team in group C. But if the 4 best third place teams are from groups A, B, D and E, then the winner of group A would play the third best team in group D.

This made things a little complicated to calculate which team progresses and who they will play in the last 16. On the *League Tables* sheet you can see a league table for the third place teams. The four best groups are calculated and the table below is formulated.

On the *Knockout Stages* sheet a VLOOKUP function is then used to return the correct team from this table.

I have previously written a tutorial on exporting all the sheets of a workbook to PDF using VBA. This macro will allow you to be more specific as to what sheets are exported. It will also export to a single PDF file, rather than a separate file for each sheet.

The key to the macro is to select the sheets that you want to export first. In the code below this is done with the following line.

Sheets(Array("Bradford", "Kettering")).Select

This references the sheets using their name. You could alternatively select the sheets using their index number like below.

Sheets(Array(2, 4, 5)).Select

Another alternative may be that the user selects the sheets in Excel, and then the macro is run. In this common scenario of only exporting the selected sheets, you would not need this line at all so it can be removed from the macro.

This VBA code can be copied and pasted into a module for the workbook. Change the code to work for your situation.

Sub ExportAsPDF() Dim FolderPath As String FolderPath = "C:\Users\Trainee1\Desktop\PDFs" MkDir FolderPath Sheets(Array("Bradford", "Kettering")).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & "\Sales", _ openafterpublish:=False, ignoreprintareas:=False MsgBox "All PDF's have been successfully exported." End Sub

The ExportAsFixedFormat method is used to perform the export to PDF. The exported file has been saved as *Sales* in this example.

The MkDir function is used to create a new folder to save the PDF into. This creates the folder in the path specified in the macro.

If you were hoping to export sheets from multiple workbooks saved somewhere in your network, then check out this tutorial on looping through the files of a folder using VBA. By combining the code from the two macros you could export many PDF’s with one click of a button.