0

I want to align to left a selection. I have a column with a header. The selection begins below the header.

I have something like :

range("C6", range("C6").end(xldown)).select
activecell.horizontalAlignment = xlleft

the selection works, but i do not know why the allignment does not. Please help.

Thank you.

FunThomas
  • 23,043
  • 3
  • 18
  • 34
Ghost2020
  • 1
  • 1

1 Answers1

0

There is a difference between the Selection and the Activecell. Activecell is always only one cell - the cell that has the focus. With your select-statement, you change the Activecell to the first cell of the selection - in your case C6. Your alignment statement just sets the alignment of that cell.

You could change your code to selection.horizontalAlignment = xlleft. However, before you do that, you should understand that it is not necessary to select anything in VBA and you should avoid it for many reasons. Please read How to avoid using Select in Excel VBA

You could write

range("C6", range("C6").end(xldown)).horizontalAlignment = xlleft

I still don't like that because it assumes that the sheet you're working with is active (which is not necessary either), but it leads to far for this answer to explain all the details.

FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • Big Thanks Thomas. It works. I will try and not use the selection. I know it is a pain. Also, i am doing all calculations each sheet separately, and activate the next one when needed. This is how i know now :). Many thanks again for your suggestions. – Ghost2020 May 05 '20 at 08:36
  • You don't need to active a sheet to do calculation on it - and you should avoid it either. – FunThomas May 05 '20 at 08:38
  • I will keep this in mind. I am learning from scratch, and every hint is a step forward for me. Thank you – Ghost2020 May 05 '20 at 08:42