VBA - Is This Possible ?

GeyikBaba

New Member
Joined
Jun 7, 2011
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Excel 2010:

I have not used VBA much in Excel, but a lot in Access.

Is it possible to write a function like:

Function AddTwoCells( lA as Long, lB as Long) as Long

AddTwoCells = lA + lB

End Function

Then call the function by entering =AddTWoCells(A2, B2) into cell C2, for example.

I have a complicated string concantenation to perform over a couple of hundred rows. The =IF() function would work, but would be way to cumbersome and hard to maintain.

Looking at my book and at the internet, all I find is code which is run by the 'Run' command.

The idea would be to paste the function/formula into the 200 cells, then special copy the resulting values manually into a different file.

When I try this, I just get the #NAME error in the cell - it's not seeing the function even though I've declared it as public.

Thanks
Mike Thomas
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Thanks for your reply. I was just using the A2 & B2 as an example. As I mentioned in my email, the real function will be far more complex than that, that's why I want to define a function.

My problem is not the VBA code, it is getting the cell to be able to 'see' or access the function.

If I could get this simple function to worj, then I can get teh complex one to work.

Mike Thomas
 
Upvote 0
You're on exactly the right lines. You pass the parameters to the function and the value of the function is returned to the cell.

Try it with your simple function first to satisfy yourself that it works, then write your more complex function. If you get stuck, come back here and there will be any number of people willing to help you.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top