Results 1 to 6 of 6

Thread: How to format tables in excel from vb6

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    211

    How to format tables in excel from vb6

    Hi i'm making a program where the user can export his data from msflexgrid to ms excel. here's my code:


    Code:
    Private Sub Export_Click()
    Dim intresponse As Integer
    
    intresponse = msgbox("Do you want to save your record, before exporting it?", vbYesNo)
    
    If intresponse = vbYes Then
        Call Sayb
    Else:
    
    Dim therows As Integer
    Dim thecols As Integer
    Dim Gridstyle As Integer
    Dim worksheetname As String
    
    Gridstyle = 1
    
    Dim objxl As New Excel.Application
    Dim wbxl As New Excel.Workbook
    Dim wsxl As New Excel.Worksheet
    Dim introw As Integer 'counter
    Dim intcol As Integer  'counter
    
    therows = MSHFlexGrid1.Rows
    thecols = MSHFlexGrid1.Cols
    
    If Not IsObject(objxl) Then
        msgbox "You need Microsoft Excel to use this function", _
           vbExclamation, "Print to Excel"
        Exit Sub
    End If
    
    On Error Resume Next
    
    ' open Excel
    objxl.Visible = True
    Set wbxl = objxl.Workbooks.Add
    Set wsxl = objxl.ActiveSheet
    
    ' name the worksheet
    With wsxl
        If Not worksheetname = "" Then
            .Name = worksheetname
        End If
    End With
        
    ' fill worksheet
    For introw = 1 To therows
        For intcol = 1 To thecols
            With MSHFlexGrid1
                wsxl.Cells(introw, intcol).Value = _
                   .TextMatrix(introw - 1, intcol - 1) & " "
            End With
        Next
    Next
    
    ' format the look
    For intcol = 1 To thecols
        wsxl.Columns(intcol).AutoFit
        'wsXL.Columns(intCol).AutoFormat (1)
        wsxl.Range("a1", Right(wsxl.Columns(thecols).AddressLocal, _
             1) & therows).AutoFormat Gridstyle
    Next
    'headers
    
    wsxl.PageSetup.CenterHeader = "GS1 Phils. Product List"
    wsxl.PageSetup.LeftHeader = "Enter Company Name,Fax Number, Tel. Number, Contact Person and Date Submitted"
    wsxl.PageSetup.LeftFooter = "&D"
    wsxl.PageSetup.RightFooter = "Page &P of &N"
    
    'format
    wsxl.Columns("D").Delete
    wsxl.Range("b2", "b50000").NumberFormat = 0
    wsxl.PageSetup.Zoom = False
    wsxl.PageSetup.FitToPagesTall = 1
    wsxl.PageSetup.FitToPagesWide = 1
    wsxl.PageSetup.Orientation = xlLandscape
    'wsxl.PageSetup.PaperSize = xlPaperA4
    wsxl.PageSetup.PaperSize = xlPaperLegal
    
    End If
    End Sub
    attached is an example of what i need. the text with red background is the format that i need to add.
    Thank you so much!
    Attached Images Attached Images  

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