tags:

views:

20

answers:

1

I am copying a worksheet programmatically by filling in a blank sheet. One column is a dollar amount. In the original sheet, it's right aligned. In the new sheet, it's left aligned. When I do format cells to look at the format, both are Number tab = General and horizontal alignment = General.

What's making them have two different kinds of horizontal alignments? I am assuming it's inferring the data type from the original one. How can I do the same in Excel for the other?

A: 

If the Number Format of the cell is General and Alignment hasn't been specifically set to anything, then text gets left-aligned and numbers get right-aligned.

However, you can deliberately set the alignment of any range, independently of the number formatting.

For example:

ActiveWorkbook.Worksheets("MyWorksheetName").Range("A1").HorizontalAlignment = xlRight

or

ActiveSheet.Range("B2:C7").HorizontalAlignment = xlCenter

or

ActiveCell.EntireColumn.HorizontalAlignment = xlLeft

Feel free to post some code if you need more detailed help.

but the question is when both are numbers, why would one be right aligned and the other left aligned when both have alignment as General. I don't want to use any code. I can just modify the text alignment. I wanted to understand why Excel is treating them differently. Is there a setting to define a column data type as number (beside the cell formatting option)?
Tony_Henrich
You can override the General number format with a specific Horizontal Alignment format, using Format Cells or using the VBA equivalents. Once you set the Horizontal Alignment, then the General number format becomes irrelevant. Does that make sense?