Results 1 to 4 of 4

Thread: Insert data after exportation

  1. #1

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Insert data after exportation

    After I manage to export my data to excel sheet, how do I specify that I would like to insert a text under the last data of the last page? Not at every page but at the page where the last data is at, at the bottom.

    this is my code:
    VB Code:
    1. Private Sub cmdReport_Click()
    2. Dim oRs As adodb.Recordset
    3.     Dim oCnn As adodb.Connection
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    6.     Dim oSW As Excel.Worksheet
    7.     Dim i As Integer
    8.     'Connect to your Access db
    9.     Set oCnn = New adodb.Connection
    10.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    11.     App.Path & "\Car.mdb;Persist Security Info=False"
    12.     oCnn.Open
    13.  
    14.     'Create your recordset
    15.     Set oRs = New adodb.Recordset
    16.     oRs.Open "SELECT Custid, CustCom, CustName FROM Table1 ", oCnn, adOpenKeyset, adCmdText
    17.     'Now you will have only 2 columns listed
    18.     'Add the field names as column headers (optional)
    19.    
    20.     'Create an instance of Excel and add a new blank workbook
    21.     Set oApp = New Excel.Application
    22.     Set oWB = oApp.Workbooks.Add
    23.     oApp.Visible = True
    24.     'Add picture at left header
    25.     Set oSW = oWB.Worksheets("Sheet1")
    26.     With oSW.PageSetup.LeftHeaderPicture
    27.     .FileName = (App.Path & "/pic1.jpg")
    28.     .Height = 50
    29.         End With
    30.     oSW.PageSetup.LeftHeader = "&G"
    31.     oSW.PageSetup.HeaderMargin = 30
    32.     oSW.PageSetup.TopMargin = 90
    33.     'Page Orientation
    34.     oSW.PageSetup.Orientation = xlLandscape
    35.     'Resize Columns
    36.     oSW.Columns("B:B").ColumnWidth = 35
    37.     oSW.Columns("C:C").ColumnWidth = 21
    38.     'Specify alignment
    39.     oSW.Columns.HorizontalAlignment = xlLeft
    40.    
    41.     'Add the field names as column headers (optional)
    42.     For i = 0 To oRs.Fields.count - 1
    43.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    44.     Next
    45.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    46.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs, , MaxColumns:=3
    47.        
    48.     oRs.Close
    49.     Set oRs = Nothing
    50.     oCnn.Close
    51.     Set oCnn = Nothing
    52.     Set oWB = Nothing
    53.     Set oApp = Nothing
    54. End Sub
    I think I'm starting to like programming, but I'm still far from good

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Insert data after exportation

    Code:
        oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs, , MaxColumns:=3
    
            '... Insert new code here        
        Dim aRow As Long
        'Find the bottom Data Row and go down 1 more row
        aRow = oSW.UsedRange.Rows.Count + 1
        'Use your own Column ID and Text
        oSW.Cells(aRow, "C").Value = "MyText"
            '... Continue your code
    
        Set oSW = Nothing
        oRS.Close
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Insert data after exportation

    Great! Thanks for the help. May I know where I can find reference for the arow function and many more solutions to Excel?
    I think I'm starting to like programming, but I'm still far from good

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Insert data after exportation

    Yanty ...

    aRow is not a function ... it is just a variable of Type Long (per the 'Dim' statement).

    If you want to learn VBA, there are a lot of books available. I use (and like!) "Definitive Guide to Excel VBA", Kofler (a! Press) for technical details. "Professional Excel Development", Bullen, Bovey, & Green (Addison Wesley) is good for general and advanced VBA programming concepts and techniques.

    Of course, you are always welcome to post on the forums!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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