cancel
Showing results for 
Search instead for 
Did you mean: 

VB code for Excel cell text to Horizontal Center Alignment

Hi,

I need VB code for Excel cell text to Horizontal Center Alignment.

Below is the code i am currently using which is not a complete one as some content and dependencies seems to be missing.

Dim wb, excel, Range As Object

Try

 

wb = GetWorkbook(Handle, Nothing).ActiveSheet

excel = wb.Application

 

Range = excel.Selection

Range.HorizontalAlignment = xlCenter

 

Finally

wb = Nothing

excel = Nothing

Range = Nothing

End Try

Kindly need your help in this.



------------------------------
Pasupuleti Mounika
Senior RPA Developer
Genpact India LLP
Asia/Kolkata
------------------------------
2 REPLIES 2

Hi

I think you are almost there with your code but since this is vb you will likely need to change the xlcenter to enumeration instead. The below link will give you a list of alignments which should hopefully help you out with this.

https://learn.microsoft.com/en-us/office/vba/api/excel.xlhalign

------------------------------
Michael ONeil
Technical Lead developer
NTTData
Europe/London
------------------------------

Thank you Michael for quick response. It worked.

------------------------------
Pasupuleti Mounika
Senior RPA Developer
Genpact India LLP
Asia/Kolkata
------------------------------