How to write a simple visual basic for applications (VBA) program in Microsoft Excel to communicate with a vector network analyzer (VNA)
In this example I will show you how to use Microsoft Excel to connect and communicate with a network analyzer via a standard GPIB connection. To do this we will use Microsoft Excels built in visual basic for applications (VBA) programming language. If you dont have experience with VBA programming dont worry. This is a step-by-step tutorial, which assumes you have zero experience with VBA.
Let us start with the prerequisites. First, for this program to work, you need to connect your PC to the network analzyer with a GBIP cable. Obviously, most PCs dont have a GPIB connection in the back, so the easiest solution is to use a USB to GPIB adaptor from Agilent. Next, you need to install the Agilent IO Libraries Suite 15.5.1 or a later version on you PC. If you install earlier versions of the Agilent IO Libraries Suite, this program may not work. At the time of this writing, the latest library suite is available free-of-charge from Agilents website. The particular version of the Agilent IO Libraries I use here is shown in the following information box:

For this example, I am using Microsoft Windows XP with Microsoft Excel 2002. If you are using a different version of Microsoft Windows and Excel, the screenshots below might look different from what you see on your screen.
To begin, open a new workbook in Microsoft Excel:

Now activate the Control Toolbox toolbar, by selecting the Control Toolbox in the Toolbars menu. The toolbars menu is located under the View drop down box as shown below:

The control toolbox is a suite of different interactive controls that allows you to create interactive/graphical things like push buttons, check boxes, sliders, etc., which link to subroutines in a visual basic code.

Make a command button by clicking on the command button icon as illustrated below. A command button is a push button that activates a piece of visual basic code.

Draw the command button as shown:

The button is labeled with the default name CommandButton1. To change the name, right click on the command button and select Edit as illustrated below:

Rename the button to *IDN?. This is a fitting name, because this button will be used query the identification (IDN) of the network analyzer. We could have just as easily labeled the button Cheeseburger, but such a description wouldnt have been too helpful.

Now resize the box, and rearrange things so they look nicer. Enter in the cell at location C2 the name and address of the GBIP connection (in my case this is GPIB0::16). This can always be changed later to match the address of a different GBIP connection.

Now create a text box adjacent to the push button. This text box will display the model number of the network analyzer once the *IDN? push button has been clicked.

Double click on the *IDN? Command button to enter the visual basic subroutine screen

You are now peering into the VBA program. Whatever is written in the subroutine (between Private Sub and End Sub) will activate whenever the pushbutton is clicked.
Below is the code for the subroutine. Dont copy it in just yet. Lets just look at it for a second. When the user presses the pushbutton labeled IDN?, the program does the following:
1. The program connects to the VNA by calling the external subroutine ConnectToVNA. I have not shown this subroutine yet.
2. The program then writes the SICL command *IDN? to the buffer and sends it to the analyzer.
3. The program reads the analzyers response and stores it in the variable idn.
4. The program places the variable idn (which contains the VNA identification tag) in the text box by calling the external subroutine TextBox1_Change. I have not shown this subroutine yet.

A screenshot of the full code with the ConnectToVNA and TextBox1_Change subroutines is shown below:

I will leave it to you to look over the code and read the comments. The completed code, which is ready to be copied and pasted in the VBA window, is given below:
Dim idn As String 'IDN number of VNA
'Variables required for network analyzer I/O connection and data transfer:
Dim ioMgr As VisaComLib.ResourceManager
Dim instrument As VisaComLib.FormattedIO488
Dim GPIBaddress As String
Public Sub ConnectToVNA()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This subroutine connects to the VNA and opens path for data transfer
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
GPIBaddress = ActiveSheet.Cells(2, 3) 'Retrieve user specified GBIP address. you can 'see the address by opening the Agilent
'Connection Expert and checking out the list under the header "Instrument I/O on this 'PC"
Set ioMgr = New VisaComLib.ResourceManager
Set instrument = New VisaComLib.FormattedIO488
' use instrument specific address for Open() parameter i.e. GPIB0::22
Set instrument.IO = ioMgr.Open(GPIBaddress)
'''End of Network Analyzer I/O connection header
End Sub
Private Sub TextBox1_Change()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This subroutine displays the idn of the VNA in the corresponding text box.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
TextBox1.Text = idn 'display idn in text box
End Sub
Private Sub CommandButton1_Click()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This subroutine executes when the user clicks the *IDN? button
'When activated, this subroutine queries the network analyzer for its IDN
'(identification).
'The IDN is placed in cell (2,7)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Call ConnectToVNA 'Connect to VNA
instrument.WriteString "*IDN?" 'This command writes *IDN? to the buffer and
'sends it to the analyzer.
idn = instrument.ReadString() 'This command reads the analyzer's response and stores
'it int he variable idn.
Call TextBox1_Change 'Place the idn (VNA identification tag) in the corresponding text 'box
End Sub
Before this code will work, we need to do one more thing. And that is to link the code to the appropriate VISA COM library files. To do this, click on References under Tools as shown below:

This will open up the references box which allows you to reference an applications library. By installing Agilent IO Libraries Suite 15.5.1, you gain access to its library files. The particular references you need to check for this program to work are: VISA COM 3.0 Type Library and VISA COM 488.2 FORMATED I/O 1.0 as shown below:

Now go back to the main Excel window and close the control tool box. If youve done everything right, clicking on the *IDN? box should connect to the VNA, query its identification, and place this identification in the text box, as shown below:

This example provides you the foundation for writing more complicated programs to read and write data to the VNA. Basically, once you establish a connection, it is all about sending SICL commands to the VNA through the instrument.WriteString command and reading back data and status updates via the instrument.ReadString command. The SICL commands can be found in the programming manual for your particular VNA (which can in turn be found on Agilents website).
The Excel file for this example is available for download.