r/vba • • 2d ago

Discussion How to enable GPU processing in Excel VBA

I create engineering functions in Excel VBA, but want to take advantage of the GPU to speed up calculations. I’ve tried prompting Copilot to ask for help, but it seems resistant to help in that regards. It wants to direct me to creating XLL’s to accelerate calculations, but I don’t know what I’m doing with XLL’s and I think they require digital signing but my work IT doesn’t allow such things.

21 Upvotes

46 comments sorted by

16

u/kay-jay-dubya 17 2d ago edited 2d ago

Yes, this can be done with VBA, and no you dont need to make an XLL to accomplish it.

OpenCL allows you to access the GPU programatically.there is a VB6 32bit demonstration of how to do it on the vbforums sire: https://www.vbforums.com/showthread.php?898360-VB6-How-to-use-GPU-from-VB6-using-opencl

(VB6 and VBA are the same language)

If you go to the bottom of the thread, I posted theVBA 64bit version of the code for anyone who might need it.

Edit: Just to head off any comments from naysayers, OpenCL uses a DLL that ships with Windows. There is nothing that one needs to install. The working 64bit demo I posted includes the necessary 64bit API declarations.

6

u/wenzelja74 2d ago

Thank you. I captured the code and will plug it in tomorrow and check it out.

2

u/5960312 1d ago

But Thadrea said you shouldn’t do that 🤣

0

u/kay-jay-dubya 17 1d ago

Lol. But seriously - shhh.... dont say anything.... or we wll all get in trouble 😅

1

u/kay-jay-dubya 17 2d ago

Glad to hear it. Hopefully it helps with your project.

Separately, much like the excellent VBGL project mentioned above, DirectX also uses the GPU, and while I appreciate you focus is engineering and perhaps not graphics, I understand it can be useful for advanced math functions that take advantage of hardware acceleration.

And I can assure you it can be done in VBA. Let me know if it would be helpful and I will see can pull together for you.

1

u/kay-jay-dubya 17 1d ago

And how did you go?

I was thinking about the functions youre looking to use, and it is worth noting that using the GPU does not necessarily mean. That it will be faster than thr CPU, so you may end up with a hybrid approach.

3

u/personalityson 1 2d ago edited 2d ago

What kind of calculations? You can make dll function calls to BLAS and Lapack with for example Openblas or MKL library. Not sure if GPU will offer any more speed, you need to move memory to GPU and then back to CPU.

Private Declare PtrSafe Sub dgemm Lib "libopenblas.dll" (ByVal transA As String, _
ByVal transB As String, _
ByRef m As Long, _
ByRef n As Long, _
ByRef k As Long, _
ByRef alpha As Double, _
ByVal A As LongPtr, _
ByRef ldA As Long, _
ByVal B As LongPtr, _
ByRef ldB As Long, _
ByRef beta As Double, _
ByVal C As LongPtr, _
ByRef ldC As Long)

1

u/wenzelja74 2d ago

Primarily, the following:
Multi-variable Catmull-Rom interpolation
Multi-variable Derivatives & Integration
Airfoil analysis (adapting XFoil to run in Excel, plus other related functions)

3

u/personalityson 1 2d ago

Then you mostly need BLAS. Nvidia has cuBLAS for their GPUs, but a GPU would be useful if you are doing several instances of your problems in parallel. I don't think it is possible to manage from inside VBA, which is forever single-threaded.

OpenBlas uses mutli-threading on CPU for very large matrices. Speed up is x10+ or so.

I asked Claude to sketch a module for Catmull-Rom interpolation, it came up with this: https://claude.ai/public/artifacts/2a14255f-378e-4c52-87c2-f1f339816453

It should normally work if you place your libopenblas.dll file in the same folder as your Excel file.

2

u/wenzelja74 2d ago

How does this work if I don’t have an Nvidia GPU?

2

u/personalityson 1 2d ago

For openblas library you don't need GPU, works on your CPU. I was just mentioning that a GPU implementation of BLAS library also exists, used in machine learning

7

u/Almesii 2d ago

I havent tried to implement Compute Shaders for general purpose programming. But it is possible to work with the gpu via vba. I did it with freeglut to work with opengl. Maybe you will find something useful: https://github.com/Almesi/VBGL

1

u/EbbMother5325 2d ago

I guess with engineering workbooks you are stuck with excel. But instead of VBA, why not write the heavy functions in Python? Add a decorator to your Python function, call it from your workbook as a regular formula. Xlwings can do it.

Or is there an IT policy that is preventing you?

1

u/wenzelja74 2d ago

I can use =PY() in cell, but I have been creating the VBA as an Excel add-in, so that I can use the functions across any workbook or multiple files. Is there a way to create python functions as an Excel add-in?

1

u/StraightAd4907 2d ago

Why do that? Python is much slower than VBA.

1

u/Thadrea 3 1d ago

Python is often a high-level wrapper for functionality that is actually implemented in a C.

It is not slower than VBA.

0

u/Thadrea 3 2d ago

You really shouldn't be doing this in Excel. It's not designed for what you're trying to do, nor is VBA.

10

u/wenzelja74 2d ago

It wasn’t designed to be running video games either, but here we are.

I’m only running calculus-based formulas in VBA to make engineering analysis easier. I just want to tap into the VBA to augment the CPU and keep it from bogging down.

-6

u/Thadrea 3 2d ago

There aren't any video games implemented in Excel or VA that anyone outside of the tiny niche of VBA hobbyists has heard of, much less any that are worth playing. Just because something can be done doesn't mean it should be done.

You could probably also accomplish this by hacking something together in SPARC M8 assembly too, but it would be an incredibly unwise technical decision.

I'm sure, with a lot of invested time and energy, what you are describing is possible. However, it would take 1,000 times as long to figure it out and implement it than doing the same thing in a language that already has available, mature tooling for it. It would also be extremely poor business judgment; legacy tech is inevitably brittle and poor for business continuity.

5

u/5960312 2d ago

A tool’s value isn’t determined by its popularity or novelty. Comparing VBA’s native integration with Excel to assembly language ignores the deployment context entirely. Claiming it takes 1,000 times longer is pure speculation, and assuming legacy technology is inherently brittle confuses age with poor engineering. Sound technical decisions consider fitness for purpose, existing infrastructure, maintainability, and actual business requirements, not personal preferences for newer languages.

-4

u/Thadrea 3 2d ago

In business, a tool's value is partly determined by its popularity.

The Dvorak keyboard, for example, allows most people to type significantly faster than QWERTY keyboards. Yet almost no one actually uses them, because few people know how and there isn't enough of a benefit to retraining employees. Moreover, a company that decided to adopt Dvorak broadly would have to pay that retraining cost again for virtually every new employee hired.

That is, of course, assuming the tool is even "better" in the first place. Excel has a broadly deployed user base, but very few of them are doing anything in VBA specifically.

It is also extremely unlikely that that will change when VBA is a proprietary language that is part of a proprietary application and said language is no longer even being actively supported by the company than made it.

1

u/kay-jay-dubya 17 2d ago

You keep using words and phrases like "should", "tiny niche", "extremely poor business judgment", "brittle".. oh hang on, that a very LLMy word... is that what's going on here?

I see that the "1,000 times" calculation has been reasonably described by another redditor as 'speculation'. The first word that came to my mind was "fabrication" , but whatever the case, it's still utterly wrong.

As is your comment about video games not having been implemented in VBA. Categorically untrue. But as to whether or not these games are "worth playing" is frankly anyones guess - I have no earthly idea what that could possibly be, but you clearly have gone through the process of reaching a determination on these points - so what game is worth playing?

-1

u/Thadrea 3 2d ago edited 2d ago

You keep using words and phrases like "should", "tiny niche", "extremely poor business judgment", "brittle".. oh hang on, that a very LLMy word... is that what's going on here?

If you do not know what these words mean, there are multiple dictionaries on the Internet. Look them up.

https://en.wiktionary.org/wiki/Wiktionary:Main_Page

I see that the "1,000 times" calculation has been reasonably described by another redditor as 'speculation'. The first word that came to my mind was "fabrication" , but whatever the case, it's still utterly wrong.

It's actually probably greater than 1,000 times, but I decided to err on the side of caution.

What OP is trying to do could be implemented in Python in under 5 minutes because there are already several robust, mature options for doing that exact thing.

As a data science manager, I know from experience that work of this complexity would likely take a senior engineer at least two weeks of solid work (80 hours) to implement a rudimentary tool in VBA to accomplish a basic use case... And what is 80 hours? Oh, right, 4,800 minutes, just 3 hours short of 1,000 times.

As is your comment about video games not having been implemented in VBA.

I didn't say any hadn't been implemented in VBA. I said "any worth playing".

You can run Doom in VBA. That being said, no one who wants to play Doom is going to go out of their way to do so from a VBA port beyond the ten minutes of entertainment you get from the novelty of it.

1

u/kay-jay-dubya 17 2d ago edited 2d ago

Cute.

I've asked you where it is written down what excel /VBA should and should not be used.

What is the basis for this omniscience you seem to have? How do you know that no one would play Doom in Excel? Im aware of three published implementations of "Doom in Excel" - one of three really isnt Doom at all, and the other two require the downloading of third party DLLs, which is a non starter in corporate offices. But at least we're making progress in getting to semblance of an actual answer: Doom is not worth playing. What would be worth playing?

And you actually said "much less" any worth playing, and that omission matters.

-2

u/Thadrea 3 2d ago

I've asked you where it is written down what excel /VBA should and should not be used.

And I didn't answer you because it's not written down anywhere. Any programming language can be used for any thing. That does not make doing so a wise decision.

I am a successful professional freely offering her advice. You do are under no obligation to take it.

What is the basis for this omniscience you seem to have?

I make no claim to be omniscient.

How do you know that no one would play Doom in Excel?

Because it already exists. A video of people watching someone play it has only attracted 31k views over two years.

The most recent commit to their github repo is from 2 years ago. Players seem to number in the tens from the number of people "watching" the project.

There is, quite simply, just not very much demand or interest in this, sorry.

the other two require the downloading of third party DLLs, which is a non starter in corporate offices.

Getting caught playing Doom in a corporate office, regardless of "how" you got it on to the machine, is a non-starter in a corporate office. How you installed it would something they'd be figuring out after they fired you for playing on company equipment and while on company time.

3

u/kay-jay-dubya 17 2d ago

Well your advice was (verbatim):

You really shouldn't be doing this in Excel. It's not designed for what you're trying to do, nor is VBA.

Ok then - thank you for coming to r/vbq to share with us that pearl of wisdom.

7

u/kay-jay-dubya 17 2d ago

Where is it written what Excel/VBA should/shouldn't be used for? What are consequences if we commit such a design violation? This would've been extremely helpful information when I bought the Office software.

-2

u/Thadrea 3 2d ago

What are consequences if we commit such a design violation?

Several:

  1. The business will not be able to maintain the application after the person who wrote it leaves.
  2. The application is dependent on upstream functionality that is barely/not maintained, so it becomes a security liability. It is also prone to unexpected breakage (what software engineers call brittleness) when Microsoft makes changes to the rest of Excel.
  3. Candidly, as a data science manager, if a job candidate told me they wrote a custom Excel add-in to handle GPU-aided data processing, that would actually make me less likely to hire them. While it would tell me that they are creative and intelligent, it would also tell me that they don't have a good handle on how modern data stacks work, probably do not have a good understanding of how to work as a member of a data team and will likely need more hands-on oversight from me than I actually want to provide. It works against your career, not for it.

This would've been extremely helpful information when I bought the Office software.

,,,OK? I did not make your purchasing decisions for you. If you are feeling buyer's remorse, that is between you and Microsoft.

1

u/EbbMother5325 2d ago

Agree. I think it’s cool they figured it out but it doesn’t mean it needs to exist. Why not hand this off to a process written in Python?

2

u/kay-jay-dubya 17 2d ago

Can't install python on the work laptop.

0

u/Thadrea 3 1d ago

That is really the crux of the problem, isn't it?

0

u/kay-jay-dubya 17 1d ago

What is this problem you speak of?

0

u/Thadrea 3 1d ago

Not being able to install Python on the work laptop.

0

u/kay-jay-dubya 17 1d ago

If it were the case my universe of possible solutions was limited to "How do I do this in Python?", then yes - I would agree that this is a problem.

However, my universe is not limited to python (as you might surmise from the title of this subreddit), and so the IT departments prohibition on the installation of binaries on work laptops hasn't presented itself as a problem thus far.

But thanks for the concern.

1

u/Thadrea 3 1d ago

My observation has been that while there are some genuine VBA hobbyists, these sorts of really convoluted VBA asks are often really artifacts of problems with the management of the person's organization.

The person is trying to do work that could be more easily done with another language or tool (e.g., Python), but they lack the ability to actually do it in that way because their organization isn't equipping them for success in the work they are doing.

Most of the time, the organization just doesn't understand the technical needs for or the business use the person's work is satisfying. I doubt there's many (any?) business decision-makers spinning up stories in Jira (or the equivalent) for writing a VBA library that leverages GPU for computation... few would even know what that means, let alone how to articulate it into a tangible development ask.

So the question that gets asked becomes "how do I do this in VBA?" when I honestly feel the better question would be "how do I get my company to authorize better tools?" or perhaps, failing that, "how do I get a better job?"

2

u/ankokudaishogun 1d ago

Possible, but sometime there is no real choice and one has to do with what they have.

"Get a better job" is also not necessarily practicable in the available timeframe.

Therefore, while pointing out VBA is not the best tool for a job is appreciated, it can be perceived as dismissive.

1

u/EbbMother5325 1d ago

Yes this is also what I see with clients. People come up with impressive but questionable duck tape hacks to work around an IT restriction.

2

u/excelevator 10 1d ago

Hello, this is r/VBA for VBA questions and solutions.

We can assume OP at this stage has made the required decision on platform

There is no need to belabor your opinion in this thread where others do not agree with your take.

Move on, find another sub reddit to argue.

2

u/kal_at_kalx_net 2d ago

The best way to get high-performance numerical computing is to use the ancient Excel C SDK and the FP datatype to get a pointer the doubles inside of Excel. I wrote xlladdins/xll24: Excel add-in library to make it simple to do this.

Here is an example of how to call BLAS from Excel xll_lapack/xll_blas.cpp at master · xlladdins/xll_lapack

You don't need signing to run on your personal computer. You might have to at work depending on your IT security settings.

1

u/Alternative_Corgi_62 2d ago

The worst part about VBA is that it sure the code inside the. xlsm document, and there's is no straight forward way to maintain it outside of Excel. Even tried to place your code in git / tfs?

1

u/kay-jay-dubya 17 1d ago

This is certainly true in the past, but not anymore - there are quite a few solutions that have been developed recently that allows you to easily and quickly extract VBA code from Office files without having to open the application at all: https://github.com/kallunWillock/vbaxray

The next version of the class allows you to add/revise/delete code to/from an office file.

1

u/MerryWalrus 2d ago

You should have switched out of excel and vba a long time before your solution got this complex. The best time to do it is right now.

-7

u/JoseLunaArts 2d ago

GPUs are used for image processing. Unless you manage to use image processing algorithms to approach functions, a GPU is useless for Excel. AI works in ways that are very similar to image processing. This is is why AI can use GPUs.

2

u/Jonathan_Is_Me 2d ago

Not quite...
Even though "Graphical" is literally in the word GPU, it's actually more accurate to describe them as Parallel Processing Units.

https://en.wikipedia.org/wiki/General-purpose_computing_on_graphics_processing_units

https://en.wikipedia.org/wiki/Parallel_computing