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

Home Community Example Programs Example Code Document

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Search instead for

Did you mean:

ASK A QUESTION

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

by:Anthony_F

‎12-06-201112:42 PM

Last Edited by: Anthony_F ‎01-30-201709:28 AM

Document options

  • Subscribe to RSS Feed
  • Mark as New
  • Mark as Read
  • Bookmark
  • Subscribe
  • Printer Friendly Page
  • Report to a Moderator

Code and Documents

Attachment

DAQmxVBA.xls ‏41 KB

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.

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

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

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

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

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

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

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

A Visual Basic button appears in the title bar

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

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

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

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

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

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

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

  • 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.

0Kudos

Comments

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

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

Member

on

‎10-22-201211:06 AM

Options

  • Mark as Read
  • Mark as New
  • Bookmark
  • Permalink
  • Print
  • Report to a Moderator

‎10-22-201211:06 AM

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

0Kudos

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

Anthony_F

NI Employee (retired)

on

‎10-22-201211:27 AM

Options

  • Mark as Read
  • Mark as New
  • Bookmark
  • Permalink
  • Print
  • Report to a Moderator

‎10-22-201211:27 AM

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

1Kudo

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

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

Member

on

‎10-23-201205:59 AM

Options

  • Mark as Read
  • Mark as New
  • Bookmark
  • Permalink
  • Print
  • Report to a Moderator

‎10-23-201205:59 AM

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

0Kudos

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

Anthony_F

NI Employee (retired)

on

‎10-23-201208:36 AM

Options

  • Mark as Read
  • Mark as New
  • Bookmark
  • Permalink
  • Print
  • Report to a Moderator

‎10-23-201208:36 AM

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

0Kudos

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

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

Member

on

‎10-27-201212:52 PM

Options

  • Mark as Read
  • Mark as New
  • Bookmark
  • Permalink
  • Print
  • Report to a Moderator

‎10-27-201212:52 PM

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

0Kudos

Contributors

  • Anthony_F

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

Using DAQmx with Microsoft Excel Visual Basic for Applications (VBA) (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6430

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.