[RESOLVED] Programmatically Centered Text In Excel Spreadsheet
I'm programmtically creating a brand new excel spreadsheet, in which I'm dumping the contents of a 5 column ListView control.
When creating Excel Templates, I can pre-center whatever cells I need to, but this spreadsheet is being created on the fly and I need to know how to center specific cells while I'm creating it.
The ListView will go into Columns A, B, C, D, and E and I need Column A, C and & D to be centered. How would I do that? Here is my Sub
VB Code:
Private Sub DisplayReport(pstrLocation As String, pRecordSet As ADODB.Recordset, _
Optional pblnSendToFile As Boolean, Optional pblnSendToPrinter As Boolean)
Dim lvwItem As ListItem
Dim objExcel As Excel.Application
Dim bkWorkBook As Workbook
Dim shWorkSheet As Worksheet
Dim i As Integer
Dim j As Integer
lvwAR.ListItems.Clear
Do While Not pRecordSet.EOF
Set lvwItem = lvwAR.ListItems.Add(, , pRecordSet.Fields.Item("prov_cd").Value)
Re: Programmatically Centered Text In Excel Spreadsheet
Originally Posted by RobDog888
Instead of centering each cell, how about centering the entire column at once? If not just change the .Columns to the desired Range.
VB Code:
'For a single cell...
'Workbooks(1).Sheets(1).Range("E3").Select
'For a entire column...
Workbooks(1).Sheets(1).Columns("E:E").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Since I need Column A, I'm guessing I would need to put this whole thing within my F i = Loop, and since C and D are contained within the For j = loop, I would also need to put that whole thing there are well?
Re: Programmatically Centered Text In Excel Spreadsheet
Ok, then A will be a single call and C and D will be a joined call.
VB Code:
Workbooks(1).Sheets(1).Columns("A:A").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Workbooks(1).Sheets(1).Columns("C:D").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Yes, before the part where your entering the text as the formatting will retian unless your doing a PasteSpecial which will change the formatting to what the source range was formatted as .
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Programmatically Centered Text In Excel Spreadsheet
Originally Posted by RobDog888
Ok, then A will be a single call and C and D will be a joined call.
VB Code:
Workbooks(1).Sheets(1).Columns("A:A").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Workbooks(1).Sheets(1).Columns("C:D").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Yes, before the part where your entering the text as the formatting will retian unless your doing a PasteSpecial which will change the formatting to what the source range was formatted as .
Nope, no PasteSpecials in this one. I'll give 'er a shot!
Do I actually need the enter With/End With or will just the HorizontalAlignment do it for me?
Re: Programmatically Centered Text In Excel Spreadsheet
If its a new workbook and you are not needing to change the default formatting in those other properties then no.
What I do is a format of a workbook upon it opening or creation so everything will be in place before any data is added or updated. Same with sizing of the column widths, font styles etc. too.
It formats faster if there is no data in the sheet.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Programmatically Centered Text In Excel Spreadsheet
Originally Posted by RobDog888
What I do is a format of a workbook upon it opening or creation so everything will be in place before any data is added or updated. Same with sizing of the column widths, font styles etc. too.
Ah....this is a good idea, and one I shall employ.
Re: Programmatically Centered Text In Excel Spreadsheet
Also, depending on your needs, you can set the sheet as hidden, format it, and then show the sheet if I was adding a new sheet to an existing opened workbook. You get the idea.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Programmatically Centered Text In Excel Spreadsheet
Originally Posted by RobDog888
[color=navy]Then you probably have an error somewhere that is causing the intellisense not to function.
Any ideas where that error could be? The Sub and all of its wonders works just fine. I just wanted a little formatting done
Originally Posted by RobDog888
Also, you shouldnt use the New in the dimensioning in theis line
VB Code:
Dim objExcel As [b]New[/b] Excel.Application
'Should do like...
Dim objExcel As Excel.Application
Set objExcel = New Excel.Application
Yes, I know and I have a nasty habit of doing that. It is funny. I can catch things like this when I'm looking at other peoples code, you would think I would be able to catch it when looking at mine.
Re: Programmatically Centered Text In Excel Spreadsheet
Thats it! You insulted me for the last time. I'm reporting your post first HA HA That will show you whos the Guru around here.
The short and sweet code is better if its just a few properties you need to set but when you need all of them its a pain and why I lke the with block. Still wonder why the intellisense isnt working for you.
Oh, do a reboot and that should solve it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.