Center function output

Gavin Elliot

New Member
Joined
Feb 28, 2013
Messages
1
I have a function but can't seem to figure out how to get the result to be centered within the cell.

Is it possible to get the output of this example function to be centered within the cell?

Function Add(a, b)
Add = a + b
With Selection.VerticalAlignment = xlCenter
End With
End Function
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
With Selection
.VerticalAlignment = xlCenter
End With

However, you technically shouldn't center inside the function. The function should just return the result.

Code:
Function Add(a, b)

add = a + b

End Function

sub runadd()

dim a as long
dim b as long
a = cells(1, "A")
b = cells(1, "B")
cell(1, "C") = Add(a, b)
cell(1, "C").VerticalAlignment = xlCenter

end sub

or you could

cell(1, "C").Select
With Selection
.VerticalAlilgnment = xlCenter
End With
 
Upvote 0

Forum statistics

Threads
1,222,564
Messages
6,166,818
Members
452,074
Latest member
Alexinho

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