Changing cell value through VBA

Krevis

New Member
Joined
Aug 3, 2016
Messages
5
I have empty worksheet where I created a module with following
Code:
Function ChangeValue()
Range("B5").Value = "Hello"
End Function

It was my belief that entering =ChangeValue() on A1 would be enough to change value on B5 to "Hello"
But instead cell focus simply move to B1 and doesn't do anything further

Number of searches on google turned up different way to phrase Range("B5").Value
such as Cell(2,5).value =
but it also simply move the cell focus to A2

What are some other troubleshooting steps? What's the correct way to change the content of specific cell?

The reason for using function is that I was passing a parameter into a function on a different problem but got stuck with a same problem where specified cell content wouldn't change.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
A function called from a sheet cannot change another cell, in exactly the same way that a regular Xl function cannot change other cells.
 
Upvote 0
If you wanted to write it to a specific cell, you would use a Sub Procedure and not a Function, i.e.
Code:
Sub ChangeValue()
    Range("B5").Value = "Hello"
End Function
A function just takes input (optional) and produces output (like any native built-in Excel functions, such as SUM, LEFT, etc).
So that would look something like:
Code:
Function ChangeValue() as String
    ChangeValue = "Hello"
End Function
Then you would use that like any other Excel function.
So, in cell B5, you could enter the function:
=ChangeValue()
and you would get that value.

You can also call the functions in procedures. So if you wanted to run the function and return the value in cell B5, you could so this (in conjunction with the function above):
Code:
Sub MyTest
    Range("B5") = ChangeValue()
End Sub
 
Last edited:
Upvote 0
Thank you for the detailed answer. Lit me up like a light bulb ?!

I will recode it by calling the function that returns a value
 
Upvote 0
You are welcome!
Glad we could help.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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