

If we wanted the cell to be right-aligned, we’d set HorizontalValue to -4152 set the value to -4131 to left-align the text. Now how do we center the text in one of the cells? Well, this line of code centers the text for cell B1 (that is, cell row 1, column 2): objWorksheet.Cells(1, 2).HorizontalAlignment = -4108Īs you can see, all we have to do is set the value of the HorizontalAlignment property of the cell to -4108 that will center the text in the cell. In case you’re wondering, that gives us a spreadsheet that looks like this: Therefore, we use these three lines of code to put the letters A, B, and C into cells A1, B1, and C1, respectively: objWorksheet.Cells(1, 1) = “A” Of course, a script that centers text in a cell isn’t all that impressive unless you actually have some text in that cell. We use the Add method to add a new workbook to our instance of Excel, then use this line of code to bind to the first worksheet in that workbook: Set objWorksheet = objWorkbook.Worksheets(1) To do that, the script first creates an instance of the Excel.Application object and then sets the Visible property to True that gives us a running instance of Excel that’s visible on screen. ObjWorksheet.Cells(1, 2).HorizontalAlignment = -4108Īt the risk of spoiling the suspense, we’ll tell you that the only line of code you really need to worry about is the last one the rest of the script simply creates an Excel spreadsheet and adds some data to it. Set objWorksheet = objWorkbook.Worksheets(1) Well, until now, that is: Set objExcel = CreateObject(“Excel.Application”) At any rate, it turns out that we’ve never told people how to center text in an Excel cell.
