using cell value in VBA

JoeS01

Well-known Member
Joined
Jun 25, 2005
Messages
832
It has been a very long while since I have been programming in VBA. Can anyone please give me simple examples of the following:

1. Using the value of a cell in the worksheet as a variable value in VBA,

and vice versa

2. Making the value of a cell equal to the value of a variable in VBA
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
1 - MyVariable = Sheets("Sheet1").Range("A1").Value

2 - Sheets("Sheet1").Range("A1").Value = MyVariable


Hope that helps.
 
Upvote 0
many thanks jonm01, much appreciated.

Can understand the first one OK, and in the 2nd example this is the formula I use for inserting a value in the cell from within VBA.

Taking it one step further, what formula do I put down in the Cell that will get a variable value from a function routine in VBA?
 
Last edited:
Upvote 0
In the second example, it is posting the value from the subroutine into the cell. In this case, you don't need a formula in the cell, the macro is putting the value there directly.

If you want a formula, then you need a UDF (user-defined function).

What are you trying to accomplish exactly?
 
Upvote 0
Thanks HotPepper. I am trying to get a value of a UDF function in VBA called myFn(x) in to an Excel cell.

eg., is the format for the formula in the cell something like this:

= myFn(A1) where A1 refers to a cell in a worksheet
 
Upvote 0
This is a very simple example:
Code:
Function myfn(r As Range)
myfn = r
End Function
 
Upvote 0
Yes, although depending on your function it may be more involved than that.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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