Results 1 to 28 of 28

Thread: [RESOLVED] Programmatically Centered Text In Excel Spreadsheet

Threaded View

  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.

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