Results 1 to 28 of 28

Thread: [RESOLVED] Programmatically Centered Text In Excel Spreadsheet

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [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:
    1. Private Sub DisplayReport(pstrLocation As String, pRecordSet As ADODB.Recordset, _
    2.             Optional pblnSendToFile As Boolean, Optional pblnSendToPrinter As Boolean)
    3.            
    4. Dim lvwItem As ListItem
    5. Dim objExcel As Excel.Application
    6. Dim bkWorkBook As Workbook
    7. Dim shWorkSheet As Worksheet
    8. Dim i As Integer
    9. Dim j As Integer
    10. lvwAR.ListItems.Clear
    11. Do While Not pRecordSet.EOF
    12.     Set lvwItem = lvwAR.ListItems.Add(, , pRecordSet.Fields.Item("prov_cd").Value)
    13.     lvwItem.SubItems(1) = pRecordSet.Fields.Item("prov_nm").Value
    14.     lvwItem.SubItems(2) = pRecordSet.Fields.Item("prov_fy_end_dt").Value
    15.     lvwItem.SubItems(3) = pRecordSet.Fields.Item("amend_cd").Value
    16.     lvwItem.SubItems(4) = pRecordSet.Fields.Item("amend_cat_cd").Value
    17.     pRecordSet.MoveNext
    18.    Loop
    19.  
    20. If pstrLocation = "screen" Then
    21.    frmShowAllAR.Height = 7935
    22.    frmShowAllAR.Top = 690
    23. Else
    24.     Set objExcel = New Excel.Application
    25.     Set bkWorkBook = objExcel.Workbooks.Add
    26.     Set shWorkSheet = bkWorkBook.ActiveSheet
    27.     With lvwAR
    28.         For i = 1 To .ColumnHeaders.Count
    29.             shWorkSheet.Cells(1, Chr(64 + i)) = .ColumnHeaders(i)
    30.         Next
    31. '=====> here is where Im dumping the listview to excel
    32. '=====>i need columns A, C & D centered somewhere somehow in this section
    33.         For i = 1 To .ListItems.Count
    34.             shWorkSheet.Cells(i + 2, "A") = .ListItems(i).Text
    35.             For j = 2 To .ColumnHeaders.Count
    36.                 shWorkSheet.Cells(i + 2, Chr(64 + j)) = .ListItems(i).SubItems(j - 1)
    37.             Next
    38.         Next
    39.         shWorkSheet.Columns("A:BZ").AutoFit
    40.         If pblnSendToFile = True Then
    41.            'if it already exists kill the sucker
    42.            If Dir$(App.Path & "\AllAppealsReopens.xls") <> vbNullString Then
    43.               Kill App.Path & "\AllAppealsReopens.xls"
    44.            End If
    45.              bkWorkBook.SaveAs FileName:=App.Path & "AllAppealsReopens.xls"
    46.              objExcel.Visible = False
    47.              Exit Sub
    48.         End If
    49.         If pblnSendToPrinter = True Then
    50.            bkWorkBook.PrintOut , , Copies:=1
    51.            objExcel.Visible = False
    52.            Exit Sub
    53.         End If
    54.     End With
    55.     objExcel.Visible = True
    56. End If
    57. End Sub
    Last edited by Hack; Mar 2nd, 2006 at 01:54 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    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:
    1. 'For a single cell...
    2. 'Workbooks(1).Sheets(1).Range("E3").Select
    3. 'For a entire column...
    4. Workbooks(1).Sheets(1).Columns("E:E").Select
    5. With Selection
    6.     .HorizontalAlignment = xlCenter
    7.     .VerticalAlignment = xlBottom
    8.     .WrapText = False
    9.     .Orientation = 0
    10.     .AddIndent = False
    11.     .IndentLevel = 0
    12.     .ShrinkToFit = False
    13.     .ReadingOrder = xlContext
    14.     .MergeCells = False
    15. End With
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote 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:
    1. 'For a single cell...
    2. 'Workbooks(1).Sheets(1).Range("E3").Select
    3. 'For a entire column...
    4. Workbooks(1).Sheets(1).Columns("E:E").Select
    5. With Selection
    6.     .HorizontalAlignment = xlCenter
    7.     .VerticalAlignment = xlBottom
    8.     .WrapText = False
    9.     .Orientation = 0
    10.     .AddIndent = False
    11.     .IndentLevel = 0
    12.     .ShrinkToFit = False
    13.     .ReadingOrder = xlContext
    14.     .MergeCells = False
    15. 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?
    VB Code:
    1. For i = 1 To .ListItems.Count
    2.             shWorkSheet.Cells(i + 2, "A") = .ListItems(i).Text
    3.             For j = 2 To .ColumnHeaders.Count
    4.                 shWorkSheet.Cells(i + 2, Chr(64 + j)) = .ListItems(i).SubItems(j - 1)
    5.             Next
    6.         Next

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    No, if you want columns formatted with centered text it should just be ok with the call before the loop where the cell contents get added.

    Ps, tsk, tsk, shouldnt this be in the OD forum.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    .
    Quote Originally Posted by RobDog888
    Ps, tsk, tsk, shouldnt this be in the OD forum.
    Good point. (Force of habit )
    Quote Originally Posted by RobDog888
    [color=navy]No, if you want columns formatted with centered text it should just be ok with the call before the loop where the cell contents get added
    Ok, but I don't want them all centered. ONLY A, C and D

    I do not want B or E centered. So, I would still use it before the loop where the cell contents get added?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Workbooks(1).Sheets(1).Columns("A:A").Select
    2. With Selection
    3.     .HorizontalAlignment = xlCenter
    4.     .VerticalAlignment = xlBottom
    5.     .WrapText = False
    6.     .Orientation = 0
    7.     .AddIndent = False
    8.     .IndentLevel = 0
    9.     .ShrinkToFit = False
    10.     .ReadingOrder = xlContext
    11.     .MergeCells = False
    12. End With
    13.  
    14. Workbooks(1).Sheets(1).Columns("C:D").Select
    15. With Selection
    16.     .HorizontalAlignment = xlCenter
    17.     .VerticalAlignment = xlBottom
    18.     .WrapText = False
    19.     .Orientation = 0
    20.     .AddIndent = False
    21.     .IndentLevel = 0
    22.     .ShrinkToFit = False
    23.     .ReadingOrder = xlContext
    24.     .MergeCells = False
    25. 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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    Ok, then A will be a single call and C and D will be a joined call.
    VB Code:
    1. Workbooks(1).Sheets(1).Columns("A:A").Select
    2. With Selection
    3.     .HorizontalAlignment = xlCenter
    4.     .VerticalAlignment = xlBottom
    5.     .WrapText = False
    6.     .Orientation = 0
    7.     .AddIndent = False
    8.     .IndentLevel = 0
    9.     .ShrinkToFit = False
    10.     .ReadingOrder = xlContext
    11.     .MergeCells = False
    12. End With
    13.  
    14. Workbooks(1).Sheets(1).Columns("C:D").Select
    15. With Selection
    16.     .HorizontalAlignment = xlCenter
    17.     .VerticalAlignment = xlBottom
    18.     .WrapText = False
    19.     .Orientation = 0
    20.     .AddIndent = False
    21.     .IndentLevel = 0
    22.     .ShrinkToFit = False
    23.     .ReadingOrder = xlContext
    24.     .MergeCells = False
    25. 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?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote 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.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    I get a "Method or Data member not found" on this line
    VB Code:
    1. .HorizontalAlignment = xlCenter
    What is Selection?

  12. #12
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Programmatically Centered Text In Excel Spreadsheet

    Hello,

    I once had a similar problem and sometimes the constant names aren't resolved correctly.

    You then need to use the value for example
    VB Code:
    1. .PaperSize = 8
    stands for A3 paper format.

    Kind regards,
    J

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Selection is the selected object, in this case it will be the column.

    What version are you running?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    What version are you running?
    Excel 2003

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Then you should have HorizontalAlignment available to you. Did you qualify it if you took out the with block?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    Then you should have HorizontalAlignment available to you. Did you qualify it if you took out the with block?
    Now that you mention it, none of the selections under With Selection appear in intellisense.

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Then you probably have an error somewhere that is causing the intellisense not to function.

    Also, you shouldnt use the New in the dimensioning in theis line

    VB Code:
    1. Dim objExcel As [b]New[/b] Excel.Application
    2.  
    3. 'Should do like...
    4. Dim objExcel As Excel.Application
    5.  
    6. Set objExcel = New Excel.Application
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote 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
    Quote Originally Posted by RobDog888

    Also, you shouldnt use the New in the dimensioning in theis line

    VB Code:
    1. Dim objExcel As [b]New[/b] Excel.Application
    2.  
    3. 'Should do like...
    4. Dim objExcel As Excel.Application
    5.  
    6. 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.

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Ok, eliminating a few possibilities here and not doing any bashing
    VB Code:
    1. 'Should reference excel here...
    2. Dim bkWorkBook As [b]Excel.[/b]Workbook
    3. Dim shWorkSheet As [b]Excel.[/b]Worksheet
    4.  
    5. 'Your creating another instance here...
    6. Else
    7.     Set objExcel = New Excel.Application
    8.  
    9. 'You can also do like this...
    10. Workbooks(1).Sheets(1).Columns("E:E").HorizontalAlignment = xlCenter
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    Ok, eliminating a few possibilities here and not doing any bashing
    VB Code:
    1. 'Should reference excel here...
    2. Dim bkWorkBook As [b]Excel.[/b]Workbook
    3. Dim shWorkSheet As [b]Excel.[/b]Worksheet
    4.  
    5. 'Your creating another instance here...
    6. Else
    7.     Set objExcel = New Excel.Application
    8.  
    9. 'You can also do like this...
    10. Workbooks(1).Sheets(1).Columns("E:E").HorizontalAlignment = xlCenter
    Are you insulting my code mister???

    Thats it! I'm reporting you to a Mod (providing I can find one of the lazy SOBs )

    I like short and sweet. I'm going to give this a whirl
    VB Code:
    1. Workbooks(1).Sheets(1).Columns("E:E").HorizontalAlignment = xlCenter

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    When I use this
    VB Code:
    1. Workbooks(1).Sheets(1).Columns("E:E").HorizontalAlignment = xlCenter
    on either the workbook or worksheet object I get an error message of:
    Attached Images Attached Images  

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Make sure your running the latest updates for Office. I am running Excel 2003 SP1. See if you can find it in the Object Browser too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    Make sure your running the latest updates for Office. I am running Excel 2003 SP1. See if you can find it in the Object Browser too.
    I, too, am running Excel 2003 SP1 and am using the Microsoft Excel 11.0 Object Library.

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Centered Text In Excel Spreadsheet

    Did you change the objects to the ones your using in your code?
    VB Code:
    1. Workbooks(1).Sheets(1)...
    2. objExcel.Workbooks(1).Sheets(1)...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Quote Originally Posted by RobDog888
    Did you change the objects to the ones your using in your code?
    VB Code:
    1. Workbooks(1).Sheets(1)...
    2. objExcel.Workbooks(1).Sheets(1)...
    Yes, but I think I might have changed the wrong ones. Hold on.

  27. #27

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Centered Text In Excel Spreadsheet

    Yep, that was it.

    I was using the Workbook/Worksheet object, not the Excel Object. When I used the Excel Object, everything worked just fine.

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Programmatically Centered Text In Excel Spreadsheet

    Cool, glad it finally worked out.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width