i have an excel file. How to :
1. how to aligh left the first column, center the second column, the rest of columns should be right
2. how to make auto width for all cells
Microsoft ExcelMicrosoft Office
Last Comment
Anthony Berenguel
8/22/2022 - Mon
Anthony Berenguel
rfedorov,
try this...
Sub customFormatCurrentWorksheet() 'right align all cells With Cells: .HorizontalAlignment = xlRight: End With 'center align column B With Columns("B:B"): .HorizontalAlignment = xlCenter: End With 'left align column A With Columns("A:A"): .HorizontalAlignment = xlLeft: End With 'auto width for all cells Cells.EntireColumn.AutoFitEnd Sub
Thank you so much, got the idea
last question
how to add the border around the area, i want the whole area, and numbers of rows always is different
when i record a macro it put the border around the existing data...
Anthony Berenguel
rfedorov,
I would suggest implementing conditional formatting in your worksheet so that if the cell is not blank then it gets borders.
no, there is no a chance that the cells will be blank
Anthony Berenguel
Are you saying your worksheet has no blank cells? If so, then I'm confused about your statement, "i want the whole area, and numbers of rows always is different
when i record a macro it put the border around the existing data..."
What is the existing data? And if there is not existing data, wouldn't that mean some cells are blank?
However, if its true that there are no blank cells anywhere on your worksheet, I think the best solution is still implementing conditional formatting on your worksheet. The condition does have to be if the cell is blank, it could be whatever you want it to be in order to get borders around your cells.
Roman F
ASKER
it could be 12 row of data, it could be 2 rows of data
That's what I mean, if there are two rows of data, then there are many, many blank cells in the remaining rows. However, I'm getting that you dont want to use conditional formatting so I'm playing with my solution to see if I can incorporate your needs.
Roman F
ASKER
ok,
let say i have a Range
Range("A3:C20").Select
'''
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
this is working but i want to make excel understand, that it should select all data in the sheet and put the border around it....
try this...
Open in new window