Consulting

Results 1 to 13 of 13

Thread: Solved: Last used cell

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Solved: Last used cell

    Hello. Im trying to write to file -but everytime I manually delete data from cells and file is empty(except header row) it doesn't start writing from A2, which is the next free row after the Header row. Instead it writes some wheredown column A. here is the code.

    [VBA] Set IWorkbook = Workbooks.open(FolderName & "IFiles.xls"_ Format:=xlDelimited)

    IWorkbook.Sheets(1).usedRange

    If Not IWorkbook is Nothing then
    IWorkbook.activate
    With IWorkbook.Sheets(1)

    rowsTmp = UsedRange.Rows.Count+1
    .Cells(rowTmp, 1 ) =wName
    .Cells(rowsTmp, 2) = Now
    IWorkbook.close Save Changes:=true
    End With

    End if

    [/VBA]

    Thanks in advance

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You use rowTmp at one stage, rowsTmp at another.

    ALWAYS USE OPTION EXPLICIT!!!!!!!!!!!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Sorry this is a typo both should be rowsTmp. This is a snippett of code from a larger piece so Option Explicit is used at the begininning.

    Any ideas?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Then it is because Excel still thinks it is the ld usedrange.

    Try checking from the bottom up to the last used row.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    . sorry this is still unresolved, I made a mistake. Im unsure how to do this xld.

  6. #6
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    I tried this, it still doesn't work: [VBA]rowsTmp = Range("A2" & Rows.Count).End(xlUp).Row[/VBA]

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by satyen
    . sorry this is still unresolved, I made a mistake. Im unsure how to do this xld.
    Removed from the Solved list!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by satyen
    I tried this, it still doesn't work: [VBA]rowsTmp = Range("A2" & Rows.Count).End(xlUp).Row[/VBA]
    That should be

    [vba]

    rowsTmp = Range("A" & Rows.Count).End(xlUp).Row
    [/vba]

    or

    [vba]

    rowsTmp = Cells(Rows.Count, "A").End(xlUp).Row
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Everytime the macro is run it should add to the next row down. The header row should be kept. i tried this - but no joy [VBA]rowsTmp = Cells(Rows.Count + 1, "A:2").End(xlUp).Row[/VBA]
    Last edited by satyen; 05-29-2008 at 02:41 PM.

  10. #10
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    remove the ":2".
    just copy what xld wrote. it should work

  11. #11
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    it doesn't seem to work

  12. #12
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    [VBA]
    rowsTmp = Cells(Rows.Count, "A").End(xlUp).Row + 1
    Cells(rowsTmp, "A").Select

    [/VBA]

  13. #13
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    works perfect , thank u very much!

Posting Permissions

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