. Tcl. Unicode C. Unicode C. Visual Basic 6.0. VB.NET. VB.NET UWP/WinRT. VBScript. Xojo Plugin. Node.js. Excel. Go VB.NET Examples Web API Categories. Visual Basic or VB.NET variables exercises Exercise 1: Write VB.NET code to declare a variable to store the age of a person. Then the output of the program is as an example shown below.
Visual Basic Code Snippets and Utilities In the following section, you can find a variety of Visual Basic code samples. For every sample, you can download the entire project for opening in Visual Basic 6.0. Revealing the passwords behind asterisks in Internet Explorer. Sample code The sample code in this article demonstrates how to do the following: Insert paragraphs with text and formatting. Browse and modify various ranges within a document. Insert tables, format tables, and populate the tables with data. To create a new Word document by using Automation from Visual Basic.NET, follow these steps. This code creates two MainMenu objects and Merges them upon FormLoad. Menus can be very powerful. Knowing how to use menus in Visual Basic is a crucial skill any developer should have. This article was originally published on June 21st, 2017 About the Author Hannes DuPreez.
VB.NET Excel examples
On this page you will find a set of HowTo samples that can help you to get started with add-in development for Excel 2019, 2016, 2013, 2010 and lower:
And here are a few more VB.NET examples for other applications of the Microsoft Office 2019 - 2000 suite:
Excel extensions: COM add-in, RTD server
How to develop a COM add-in for Microsoft Office (Excel, Word and PowerPoint)
This sample shared COM add-in for Excel, Word and PowerPoint shows how you can use Add-in Express for Office and .net to add custom command bars and command bar controls, place new controls onto Office Ribbon tabs, create custom task panes, handle application-level events and more.
Download COM addin
How to customize Microsoft Excel ribbon and toolbar
See how to create custom ribbons and toolbars for Excel and how to integrate with an existing Excel tab.
Download add-in
How to customize Excel main menu, context menus and Backstage view
VB.NET sample shows how to create a custom context menu for Excel 2019, 2016, 2013, 2010, 2007 and lower; how to add your own Backstage view item and how to customize the main menu in Excel 2003 - 2000.
Download sample
How to build your first Excel RTD server in VB.NET
See how to build an RTD server step-by-step in the Developer's Guide. The RTD server project is written in Visual Basic .NET. See also a video showing how to create an RTD server.
Download RTD server
Working with Excel Object model
How to handle the WorkbookBeforeSave event
How to create a custom event when Excel calculation mode changes
This VB.NET Excel addin example provides a solution for missing event that should occur then the user changes the calculation mode.
Download addin
How to find the last used cell (row or column) in Excel
This Visual Basic sample explains some reliable methods for finding the last used cell in an Excel worksheet or a range.
Download Find Last Cell add-in
How to populate Excel workbooks and ranges with arrays
Visual Basic Sample Projects
VB.NET code example shows how to use arrays to populate Excel with data; how to fill array with a query or range and insert into a worksheet, and more.
Download Populate with Arrays add-in
How to work with Excel tables & ranges using VB.NET
This sample Excel addin demonstrates how work with tables and ranges: create a new table or a range, insert a column or a row, sort, filter and more.
Download Excel Tables and Ranges add-in
How to work with Excel pivot tables
VB.NET code example shows how to automate pivot tables in Excel: create a PivotTable, add calculated fields, display or hide a field; delete, refresh or clear a pivot table; create a pivot chart and more.
Download Excel PivotTables Add-in
Advanced Excel Task Panes
How to show and hide Excel Task Panes programmatically
This Visual Basic .NET Excel sample demonstrates how to set the visibility of an advanced task pane to make it show up from the command bar.
Download VB.NET sample
How to switch between several task panes programmatically
This addin shows how you can use several custom task panes in one position and switch between them programmatically.
Download example
How to show an advanced Excel task pane dynamically
The Excel VB.NET add-in shows how to build a context-dependent custom task pane. The plug-in shows a task pane when cell A1 contains any value.
Download plug-in
How to re-size an Excel task pane in VB.NET
This Visual Basic .NET example shows how you can resize your custom form using Advanced Task Panes for Excel 2019 - 2000. To change the form size programmatically, you set the splitter to none. Otherwise, only the user can resize the task pane using the splitter.
Download sample plugin
Visual Basic 2017 Code Samples Pdf
User-defined functions
How to develop Excel Automation add-in
See how to create Excel Automation add-ins step-by-step in the Developer's Guide. The sample is written in VB.NET.
Download Automation add-in
How to develop XLL add-in
This sample project demonstrates how you can build an XLL addin in Visual Studio. You can find a detail description of this example in the Developer's Guide: How to create an XLL add-in.
Download XLL addin
Note. You can find plenty more HowTo examples on our technical blog. Be sure to check it out, we add new HowTo samples every week.
There are two Do Loops in Visual Basic: the Do While and Do Until. The Do While loops something which is true, and the Do Until loops until a certain condition is met. Create a new VB Console Application and name it Do Loops.
Do Until
Code Explained
- First we make a integer and call it myNumber, and set its default value to 0
- Then we have a Do Until Statement which loops until myNumber = 5
- Next we ask the user to pick a number between 1 and 6
- We read the user’s value and store it in myNumber
- If the number is not 5 then they get the text “Oops that’s not the lucky number”
- If they do not pick 5 then the question will be asked again. (It is an infinite loop, so it will keep looping until myNumber is 5)
- If they pick 5 then they will get the message “Yes 5, it is!” The program will close after this.
Do While
The Do While is similar to the Do Until but it evaluates something which is true.
Example
Code Explained
- First we make a integer called mysecondNumber
- We use the Do While loop and see if mysecondNumber is less than 50 (which is true)
- We then increment it by 1 until it reaches 50 (then the Do While will become false)
Operators
You can use the following operators with Do Until and Do While:
Operator | Description |
---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
<> | Not equal to |
= | Equal to |
Loop While and Loop Until
It is possible to place the While and Until after the loop so it appears as:
In this example, the Until and While are after the loop, and the code will always execute at least once. Now if you run this code, it will crash and give an overflowException: the first Loop While will execute and it will keep going until myNumber is 10, and then it will exit. The Do Until loop will then execute, and this will keep looping (infinitely) because myNumber is no longer equal to 10. The Do Loop executed once and myNumber += 1 changed its value to 11, and therefore this loop will execute until it reaches 10, but it won’t!
It is best to use Do While and Do Until instead of putting the While and Until after the loop, because they will always keep executing. The Do While/Until will not execute if its condition is false. Issues like the above example cause bugs in your application since the variable changed.
Example
This is similar to the above example, however this time when the first loop executes and reaches 10, the second loop will not execute because myNumber is already 10.
Summary
This tutorial covered the different types of Do Loops and also the operators. In addition you learned about Loop Until and Loop While.