You can use most Microsoft Excel worksheet functions in your Visual Basic statements. For a list of the worksheet functions you can use, see List of worksheet functions available to Visual Basic.
Note
Once you start to understand how to use Excel for the most basic operations–like setting up rows and columns doing simple mathematical functions, you could try creating a pivot table that helps you visualize the data you enter in different ways–columns vs. Rows, for example. Excel formulas make calculating numbers and making sense of large amounts of data simple.By knowing a few key formulas, you can do a variety of actions in Excel that will increase your productivity and decrease the risk of making calculation mistakes. We’ve put together 10 Excel formulas you need to get started. There are a lot of complicated formulas out there, but a great formula doesn’t.
Some worksheet functions are not useful in Visual Basic. For example, the Concatenate function is not needed because in Visual Basic you can use the & operator to join multiple text values.
Calling a worksheet function from Visual Basic
In Visual Basic, the Excel worksheet functions are available through the WorksheetFunction object.
The following Sub procedure uses the Min worksheet function to determine the smallest value in a range of cells. First, the variable myRange
is declared as a Range object, and then it is set to range A1:C10 on Sheet1. Another variable, answer
, is assigned the result of applying the Min function to myRange
. Finally, the value of answer
is displayed in a message box.
If you use a worksheet function that requires a range reference as an argument, you must specify a Range object. For example, you can use the Match worksheet function to search a range of cells. In a worksheet cell, you would enter a formula such as =MATCH(9,A1:A10,0). However, in a Visual Basic procedure, you would specify a Range object to get the same result.
Note
Visual Basic functions do not use the WorksheetFunction qualifier. A function may have the same name as a Microsoft Excel function and yet work differently. For example, Application.WorksheetFunction.Log
and Log
will return different values.
Inserting a worksheet function into a cell
To insert a worksheet function into a cell, you specify the function as the value of the Formula property of the corresponding Range object. In the following example, the RAND worksheet function (which generates a random number) is assigned to the Formula property of range A1:B3 on Sheet1 in the active workbook.
Example
This example uses the worksheet function Pmt to calculate a home mortgage loan payment. Notice that this example uses the InputBox method instead of the InputBox function so that the method can perform type checking. The Static statements cause Visual Basic to retain the values of the three variables; these are displayed as default values the next time you run the program.
See also
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
When it comes to Excel, there isn’t much middle ground.
You have people who absolutely love it and will sing the praises of spreadsheets all day. And, then you have the people who absolutely detest it. They’d rather lock themselves in a phone booth full of mosquitos than have to go cross-eyed looking at all of those columns and rows.
Admittedly, I used to fall into that latter group. I’d open a new Excel workbook with the best of intentions. But, after 20 odd minutes of trying to get one stupid decimal point to appear properly in its cell, I’d throw my hands up once again and claim Excel just wasn’t for me.
Then, my life experienced a major plot twist: I married a total Excel whiz—someone who literally spends his entire workday creating complicated macros and some of the most impressive spreadsheets I’ve ever seen. And, he’s made it his personal mission to convert me to his tribe of Excel-lovers (honestly, I’m surprised it wasn’t in his wedding vows).
Since then? Well, he’s made some progress. I’ve been able to put my hatred aside and recognize that Excel can actually be an incredibly powerful tool for combing through information and finding exactly what you need—provided you know how to use it correctly.
It’s that last part that trips people up. But, fortunately, Excel isn’t nearly as complicated as you’re likely making it out to be.
In fact, there are plenty of helpful tricks and tools you can utilize—whether you’re a total newbie or an established expert. Here are six things you should absolutely know how to do in Excel (and, trust me, you’ll be glad you do!).
Free Excel crash course
Learn Excel essentials fast with this FREE course. Get your certificate today!
Start free course1. Sort data
Typically, spreadsheets are useful for storing and sorting a whole bunch of information—think a contact list for 800 people that you want to invite to your company’s luncheon, for example.
Now, let’s say that you want to sort those people accordingly. Perhaps you want them listed in alphabetical order by last name. Or, maybe you want to group them together by city.
Excel makes it easy to comb through your entire data set and sort everything into a clean and easy to read spreadsheet.
Here’s how you do it:
- Highlight the entire data set you want to sort (not just one column!) by either dragging your cursor across all of the cells or clicking the triangle in the upper left of your spreadsheet to select the entire thing.
- Hit the “Data” tab.
- Click the “Sort” button.
- Select how you want to sort your data (in the example below, I sorted by city!).
- Hit “OK.”
Then, your data will be sorted accordingly—in this case, alphabetical order by city.
IMPORTANT NOTE: It’s important that you select the entire data set you want to sort, and not just one column. That way, your rows will stay intact—meaning, in this case, the correct address will stay with the appropriate person.
Had I just selected the first column, Excel would’ve sorted only that one column alphabetically, making the addresses a mismatched mess.
2. Remove duplicates
It’s inevitable: When you’re working with a large dataset, there are bound to be a few duplicates that sneak their way in.
Rather than getting bleary-eyed and frustrated by scrolling through that entire spreadsheet and looking for them yourself, Excel can do all of that legwork for you and remove duplicates with the click of a button.
Here’s how you do it:
- Highlight the entire data set.
- Hit the “Data” tab.
- Click the “Remove Duplicates” button.
- Select what columns you want Excel to find duplicates in.
- Hit “OK.”
IMPORTANT NOTE: Be careful that you choose enough qualifiers to weed out the true duplicates. For example, if I had just selected to remove duplicates in only Column A above (meaning Excel would’ve looked for duplicates of “Oprah”), I would’ve deleted one Oprah that indeed had the same address, but one that had a different last name and address altogether (a different Oprah entirely!)
The bottom line is, utilize enough information so that you’re removing rows that are true identical copies of each other—and don’t just share one similar value!
Want to learn more?
Take your Excel skills to the next level with our comprehensive (and free) ebook!
3. Basic math functions
Stop reaching for that calculator—Excel can handle all sorts of math functions for you! All you need to do is enter a few simple formulas.
Think that sounds like it’s way beyond your Excel knowledge? Think again. Trust me, if I can figure this out, so you can you.
Here are the basic formulas you’ll want to know:
- Addition: Type “=SUM” in a blank cell where you want the total to appear, click the cells you want to add together, and then hit “Enter.”
- Subtraction: Type “=” in a blank cell where you want the difference to appear, click the cell you want to subtract from, type “-”, click the cell you want to subtract, and then hit enter.
- Multiplication: Type “=” in a blank cell where you want the total to appear, click the cell for a number you want to multiply, type “*”, click the cell for the other number you want to multiply, and then hit enter.
- Division: Type “=” in a blank cell where you want the remainder to appear, click the cell for the number you want to divide, type “/”, click the cell for the number you want to divide by, and then hit enter.
Listen, I know these are a little confusing to put down in words. But, give them a try for yourself and I’m positive you’ll quickly see that they aren’t complicated at all. Here’s a look at what the SUM function looks like in practice:
INSIDER TIP: If you want to drag the same mathematical formula across a row, you can! After entering the formula into one cell, click that cell where the total appeared, click the little green box that appears in the lower right-hand corner, and drag it across the rest of the row where you need that formula to be applied.
Voila—it’ll happen automatically! You’ll be able to crunch numbers in different columns, without needing to enter the formula again and again.
4. Freeze panes
There’s nothing worse than scrolling through a huge spreadsheet that requires you to continuously go back up to the top to see what your column headers are.
How Do You Use Excel Spreadsheets
Fortunately, you can make your column headers and your row numbers stay right where they are—meaning you can always see them, no matter how far down the spreadsheet you go. You can do this by using Excel’s handy “freeze panes” feature.
Here’s how you do it:
- Click on the row underneath your column headers.
- Click on the “View” tab.
- Click the “Freeze Panes” button.
Scroll down and across your spreadsheet, and you’ll see that the information you need is always right there within view!
5. Insert current date
Sick of glancing at your calendar or the bottom of your computer monitor in order to get today’s date and enter it in your spreadsheet?
Excel can do it for you—with just one easy keyboard shortcut. Here it is:
Ctrl + ;
Put your cursor in the cell where you want the date to appear, use that shortcut, and Excel will automatically fill in today’s date for you. Easy peasy!
IMPORTANT NOTE: Dates entered using that function are static, meaning they won’t change as your spreadsheet ages!
6. Make the same change across worksheets
When you’re working with multiple tabs, it’s a hassle to comb through them all and make the same change over and over again. Fortunately, you don’t have to!
You can select the appropriate sheets in your workbook where that change should appear. Make the change once, and it’ll be applied across all of the sheets you selected.
Here’s how you do it:
- Hold the “Command” key on your keyboard (or “Control” if you’re using a PC).
- Select the appropriate tabs of your workbook.
- Make the necessary change to one cell.
- Check to make sure it applied across all of your worksheets.
Want to see this in practice? For simplicity’s sake, let’s assume I got married to Aaron Rodgers (hey, a girl can dream!). As a result, I changed my last name from “Boogaard” to “Rodgers.” Since my name appears in numerous different tabs of this spreadsheet, I’d use this handy trick to only have to enter my new last name one time.
And that's what you need to know how to do in Excel
I get it—Excel can feel a little intimidating. But, once you start playing around, you’ll begin to become more and more comfortable and quickly begin to realize just how much easier it can make things for you.
Get your start by mastering these six basic Excel tricks, and you’ll be on the path to becoming a total Excel whiz in no time!
Want to present your Excel data in a professional way? Check out these Excel presentation tips.
Free Excel crash course
Learn Excel essentials fast with this FREE course. Get your certificate today!