Results 1 to 6 of 6
  1. #1
    5 Star Lounger access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    623
    Thanks
    55
    Thanked 47 Times in 44 Posts

    Adding lines to worksheet via VBA

    I have some VBA in an Excel file, which reads data from a file and (after parsing it) adds the results to a worksheet. The code I'm using to do this is

    Worksheets("Sheet2").Cells(Count, 1) = Title
    Worksheets("Sheet2").Cells(Count, 2) = "08/01/2014"

    where count is the line number of the first blank line and is manually changed for each file read. What I want to do is to have the equivalent of Access's addnew, so that it automatically adds the new data in the first blank line. Is this possible in Excel?

    The VBA has been converted from an Access database. I'm currently using Office 2003 for this, but it will be running on Office 365.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,503
    Thanks
    212
    Thanked 853 Times in 785 Posts
    Access,

    This VBA will find the last USED row in an excel sheet: lLastRow = Cells(rows.Count, "A").end(xlup).row

    It should work as written for you since you are placing the Title in Col A. You'll only have to add 1 to lLastrow to get the 1st empty row where you want to put your values.

    To integrate right into your code you could use: Count = Cells(rows.Count, "A").end(xlup).row + 1

    Note: It isn't a good idea to use Count as a variable as it is also an excel property, as can be seen in the code above, and can lead to ambiguities and problems.

    HTH
    Last edited by RetiredGeek; 2014-08-08 at 08:28.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    access-mdb (2014-08-08)

  4. #3
    5 Star Lounger access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    623
    Thanks
    55
    Thanked 47 Times in 44 Posts
    Thanks - I will change the count variable straight away! And add that code - just the job.

    One other question (I hope it doesn't need a new thread). The date in my code fragment is 8th January (UK format). The script puts it into the Office 365 Excel as 1st August (so treating it as US format). The spreadsheet display dates in UK format, so where can I tell the VBS that it's UK format? It's OK in Excel 2003.

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,503
    Thanks
    212
    Thanked 853 Times in 785 Posts
    Access,

    Don't have Office 365 but see if this Article helps.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,549
    Thanks
    3
    Thanked 149 Times in 142 Posts
    Try specifying the date unambiguously

    Worksheets("Sheet2").Cells(Count, 2) = "8 January 2014"
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #6
    5 Star Lounger access-mdb's Avatar
    Join Date
    Dec 2009
    Location
    Oxfordshire, UK
    Posts
    623
    Thanks
    55
    Thanked 47 Times in 44 Posts
    Quote Originally Posted by RetiredGeek View Post
    Access,

    This VBA will find the last USED row in an excel sheet: lLastRow = Cells(rows.Count, "A").end(xlup).row

    It should work as written for you since you are placing the Title in Col A. You'll only have to add 1 to lLastrow to get the 1st empty row where you want to put your values.

    To integrate right into your code you could use: Count = Cells(rows.Count, "A").end(xlup).row + 1

    Note: It isn't a good idea to use Count as a variable as it is also an excel property, as can be seen in the code above, and can lead to ambiguities and problems.

    HTH
    This worked eventually RG, but it was always using the first worksheet rather than the one I wanted. After a bit of investigation, I worked out how do specify the correct worksheet. Here's my final code:

    Sub Import_scripts()

    Dim Title As String, Author As String, Copyright As String, a As String, lr As Integer

    Open "F:\Documents\V4\20130512.ssc" For Input As #1

    lr = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1

    Do
    Line Input #1, a
    If InStr(a, "<Title>") > 0 Then
    Start = InStr(a, "<Title>") + 7
    endtitle = InStr(a, "</Title>")
    Title = Mid$(a, Start, endtitle - Start)
    Worksheets("Sheet2").Cells(lr, 1) = Title
    Worksheets("Sheet2").Cells(lr, 2) = "08/01/2014"
    lr = lr + 1

    End If
    Loop Until (EOF(1))
    Close #1

    End Sub


    Thanks for your initial help, without it I would be floundering.

    Andrew, I'm using 2003 to test this and the date's fine. I will tell the person running the script (in Office 365) to format the date as suggested.

Posting Permissions

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