Example Code

Using DAQmx with Microsoft Excel Visual Basic for Applications (VBA)

Code and Documents

Attachment

VBA is closely related to Visual Basic and uses the Visual Basic Runtime, but can normally only run code within [most Microsoft Office applications].1   VBA can indirectly use most of the code provided with the NI-DAQmx Examples for Visual Basic 6.0.  This example uses parts of the Read Dig Chan example source code to quickly implement a VBA program utilizing DAQmx within Microsoft Excel.  Please see the attached Microsoft Excel spreadsheet to follow along with the setup instructions below.

1.0 First Open Microsoft Excel, click the Office Button and open the Excel Options window.

mnuExcelOptions.png

2.0 Navigate to Excel Options»Popular»Top options for working with Excel»Show Developers tab in the Ribbon»Ok


wndExcelOptionsPopular.png

This will add the Developer tab where you can access the Visual Basic Editor.

xlDevTabRibbon.png

2.1 You can also add a Visual Basic Editor command button at the top of the window by navigating to Excel Options»Customize»Choose commands from»Developer Tab»Visual Basic»Add»Ok

wndExcelOptionsCustomize.png

A Visual Basic button appears in the title bar

xlVBicon.png

Now you will need to add in the DAQmx C reference type library.  This is installed with NI-DAQmx driver into the Windows system folder.

4.0 From the Visual Basic Editor, click Tools»References

mnuVBToolsReferences.png

5.0 In the References window check the NI DAQmx C API type library.  If it is not listed, browse to C:\windows\system32\nidaqmx.tlb

wndVBReferences.png

6.0 Start the example by opening UserForm1 and navigating to Run»Run Sub/User Form

wndVBEditorRun.png

  • Please also note that the DAQmxErrChk function is included in a separate module in the VBAProject.  This function is needed to retrieve the error code of any DAQmx call that fails or returns a warning.  The source code for the error checking module can be found in the NI-DAQmx Examples for Visual Basic 6.0 knowledge base article

1 http://en.wikipedia.org/wiki/Visual_Basic_for_Applications

Anthony F.
Staff Software Engineer
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
cosococo
Member
Member
on

Dear Anthony_F

I need an application similar to the one you created, run DAQmx Using Microsoft Excel with Visual Basic for Applications (VBA)

but you have to read analog input data and save in spreadsheet of excel for viewing later or real time.

The application must allow select multiple analog input channels

Please if you help me I will be eternally grateful.

Greetings

PS: my  card of data acquisition is NI USB 6008 of National Instruments

Anthony_F
NI Employee (retired)
on

Hello Cosococo,

The example I posted was taken directly from the Visual Basic 6.0 example located at C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\Visual Basic 6.0\Digital\Read Values\Read Dig Chan (Windows 7 folder).

VBA will not open the examples directly because they were saved using VB6, not VBA. You can, however, open the FRM files in notepad and copy/paste the code into an Excel/VBA form and re-create the interface manually as I did.

I suggest taking the code from this example which has already been written C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\Visual Basic 6.0\Analog In\Measure Voltage\Acq-Int Clk

Good Luck!

Anthony F.
Staff Software Engineer
National Instruments
cosococo
Member
Member
on

Dear Anthony

I tanks you, for your answer quickly, the true is que I don't hope that you answer in the same day. But now I have an other question in relation wiht my application.

In the example Acq-Int Clk of VB6 use the control ListView for acquisition of data, the problem is that control ListView isn`t appear in VBA. How I can this control?. Is posible add the control someway?. ¿What I can do?

Apart from, ¿How I can store the data that receive for channel in sheet of same application of VBA?

Thanks in advance

Greetings



Anthony_F
NI Employee (retired)
on

Hello cosococo,

A better reference for VBA programming would be a book, you should be able to find them cheaply now that .NET has taken the spot light.

You can follow this page to get the list view control into VBA, http://mindkid.blogspot.com/2011/10/adding-listview-additional-activex.html

Another google search reveals how to access cells from within VBA as well, http://www.ozgrid.com/forum/showthread.php?t=11530

Good Luck!!

Anthony F.
Staff Software Engineer
National Instruments
cosococo
Member
Member
on

Dear Anthony

Thank you very much for all your help I have been very helpful.

Although I have a problem, don't know how to solve it, took a week and tried but I don`t know, please if you could help me I would greatly appreciate it.

I have the application that works perfectly, I got it to work following your advice, but the application must be storing the data to be entering the card into an Excel spreadsheet (in the application itself).

That is the data that is displayed in ListView is you have to go stored in an Excel spreadsheet to graph later or even in real time, How I can do?, I tried a week and I can not, and I need to have it I have to present it for a class project and it depends on my approved I earnestly request you to help me.

I posted the application on the following link:   https://decibel.ni.com/content/docs/DOC-25021          ,please check it

Contributors