Excel VBA - City University Continuing Education

VBA for Microsoft Excel – Part 9

Table of Contents

1 Developer Tab in Excel 2010 /2013 1 2 Creating a command button 1 3 Spin control 2 4 Combo box 2 4.1 Creating code for the Combo box 3 5 A Scroll Bar to change the interest rate 3 6 Getting the row number of the last row 4 6.1 Row property / Column property of a range 4 7 Macros that Delete 5 7.1 Deleting Empty Rows 5 7.2 Deleting worksheet data5 2 Creating a command button To place a command button on your worksheet, select the Developer 1 Developer Tab in Excel 2010 /2013 ribbon, click the Insert button. This displays two sets of buttons, Form To display the Developer tab on the Ribbon, follow these steps: Controls and ActiveX controls. 1. click the File menu in Excel 2. choose Options (second from the bottom) 3. in the list on the left click Customize Ribbon 4. in the List Box on the right check the entry for Developer 5. Click OK

Page 1 of 6 Excel VBA - City University Continuing Education

the Name of the button and its Caption. Close the Properties window when you have finished with it.

A quick way to create code, while still in Design mode, is to double-click the button. This creates the beginning and end of a procedure, located in the sheet module for the sheet where the button was placed. 3 Spin control You can use a spin control to input data to a cell. In the following example, the cells contain arguments supplied to the PMT function which works out Note that though they look the same, you use them in entirely different payments on a loan. ways. ActiveX controls have superseded Form controls, and the differences is that you can write code for the ActiveX controls, whereas with Form controls you can only atttach a macro.

Click the command button tool from the ActiveX controls, then draw a rectangle on your worksheet to the size that you want your button.

You can of course change the values manually, but it is easier to have a control to do it. Click the Spin control tool from the Active X controls and draw a small rectangle adjacent to cell D7 Display the properties of the Spin control – the important options are: • Max value 10 • Min value 1 When you create the Command button, the Design mode button is • Linked cell D7; the Linked cell is the cell that gets its value from the control automatically turned on. In Design mode you can customise the button by • Small change value 1 setting its properties. If necessary, click the Properties button and change

Page 2 of 6 Excel VBA - City University Continuing Education

4 Combo box 4.1 Creating code for the Combo box Let’s say you have a list of values – for example, H3:I9. You might want to While in Design mode, double click the Control. This takes you to the sheet tie the price to one of the values in the list. This can be done with a Combo module. Enter the following code for the combo box’s Change event box. Private Sub cboPrice_Change() Range("D3").Value = CboPrice.Value To make life easier we will create a named range called CarList for the data End Sub in H3:I9. On returning to the sheet you should find that if you exit Design mode, selecting a value from the combo box links directly to cell D3.

5 A Scroll Bar to change the interest rate It would be very convenient to have a control that could change the interest rate in the function. A scroll bar can do this, though setting it up is a little tricky. Insert a combo box from the Active X controls. Name the combo box Click the scroll bar tool from the Active X controls, and draw the scroll bar cboPrice. in row 6, adjacent to the interest rate. Scroll bars work much like Spin buttons, but you can take larger steps. By its nature, you may want to change the interest rate by fractions of one percent. To get round the problem of only being able to increment its value by 1 (which is 100%), we need to resort to a bit of guile. Here are the other properties to change: With the scroll bar set the properties as follows: • Style 2 - fmStyleDropDownList  Max property to be 2000 • Listfillrange CarList  Small Change property to be 5 • columnCount 2  Large Change property to be 100 • BoundColumn 2 (to access prices, not car name)  Linked Cell property to be somewhere else, say H1 (you can hide it later) – the value from the scroll bar will be displayed here. Now change the formula in D6 to be =H1/10000.

Page 3 of 6 Excel VBA - City University Continuing Education

6 Getting the row number of the last row In the illustration there are three blank rows at the top. Often a macro needs to determine the row number of the last row of a worksheet, e.g., in case you want the macro to add data to the first empty row. You could use the expression ActiveSheet.UsedRange.Rows.Count and most of the time it would be adequate. For example, Sub GoToFirstEmptyRow() Dim nrows As Long nrows = ActiveSheet.UsedRange.Rows.Count Cells(nrows + 1, 1).Select End Sub

However this only works reliably if there are no blank rows at the top of the worksheet. To enhance this code we can use the Row property of the Range. 6.1 Row property / Column property of a range Row The property returns the spreadsheet row number of a range. The expression ActiveSheet.UsedRange.Rows.Count ActiveCell.Row gives 23, but the row number we need to work out is 26, because of the gives the row number of the active cell. three blank rows. The Column property is the equivalent for Columns. ActiveSheet.UsedRange.Row gives 4 ActiveCell.Column gives the column number of the active cell If we subtract 1 from that, we have an expression for the number of blank rows at the top. So ActiveSheet.UsedRange.Row - 1 gives 3, the number In the case of a selection the Row property gives the row number of the first we need to add to 23, giving 26 which is the last row number. The row in the selection. expression is therefore: Selection.Row ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count would give 4 in the illustration. Sub GoToFirstEmptyRow() (don’t confuse this with the Rows property. Selection.Rows.Count Dim nrows As Long would give 10 in the illustration – the selection has 10 rows) nrows = ActiveSheet.UsedRange.Row - 1 _

Page 4 of 6 Excel VBA - City University Continuing Education

+ ActiveSheet.UsedRange.Rows.Count If you set the ScreenUpdating property to False, a macro which needs a lot Cells(nrows + 1, 1).Select of screen updating runs faster. Just remember to set the property back to End Sub True before the end. 7 Macros that Delete

7.1 Deleting Empty Rows 7.2 Deleting worksheet data For this problem we need to start at the bottom of the data and work up. If Deleting unwanted rows of data can be a tricky procedure. Problems are we delete starting from the top then, when a row is deleted, Excel would minimised if you to start deleting at the bottom of the range and work up. have to recalculate all the references below the deleted row – but some of Consider the illustrated worksheet; suppose you wish to delete rows where these will be empty and also need to be deleted so it’s a waste of time. the value in column B is ‘OR’ Therefore a program is more efficient if it starts with the last row of the data and works upwards.

Sub DeleteEmptyRows() Dim LastRowNum As Long 'Long in case there are more than 32767 rows Dim i As Integer

LastRowNum = ActiveSheet.UsedRange.Row – 1 _ + ActiveSheet.UsedRange.Rows.Count

Application.ScreenUpdating = False

For i = LastRowNum To 1 Step -1 If WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete A working macro couldbe as follows: End If Next i Sub Delete1() Dim i As Integer, n As Integer, counter As Integer Application.ScreenUpdating = True Cells(1).Select End Sub n = ActiveCell.CurrentRegion.Rows.Count For i = n To 1 Step -1 If Cells(i, 2) = "OR" Then

Page 5 of 6 Excel VBA - City University Continuing Education

Rows(i).Delete counter = counter + 1 End If Next i MsgBox counter & " rows deleted" End Sub The macro whould work and avoid problems. If you changed the code to delete from top to bottom (line 5 would be For i = 1 To n) you would find that macro fails to delete where the value appears in consecutive rows, e.g, row 10 in the illustration. This is because Excel rebuilds row numbers beneath the deleted row which causes a synchronisation problem with the variable i in the loop and effectively ‘misses’ one.

Page 6 of 6