Consulting

Results 1 to 7 of 7

Thread: Macro Code Overwrites Previous Data

  1. #1
    VBAX Regular
    Joined
    May 2011
    Posts
    46
    Location

    Macro Code Overwrites Previous Data

    Hello,

    The following code will only store the last [Textbox4.value].
    Is there a way to save them below each other, but without using "LastRow + 1"?


    [VBA]
    Sheets("1").Range("B" & LastRow).Value = UserForm1.TextBox2.Value
    Sheets("1").Range("B" & LastRow).Value = UserForm1.TextBox3.Value
    Sheets("1").Range("B" & LastRow).Value = UserForm1.TextBox4.Value
    [/VBA]

  2. #2
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    Try using this instead for each one[vba]Sheets("1").Range("B" & Range("B" & Rows.Count).end(xlUp).Row).Value = [/vba] The reason your's currently overwrites each time is because you are taking the value of the last row before adding any values but then the variable isn't updated, so if you had lastrow of 25 it will always remain that until you open the userform again
    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)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is wrong with Lastrow + 1?

    [VBA] With Sheets("1").Range("B" & LastRow)

    .Value = UserForm1.TextBox2.Value
    .Offset(1, 0).Value = UserForm1.TextBox3.Value
    .Offset(2, 0).Value = UserForm1.TextBox4.Value
    End With
    [/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

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,093
    Location
    LOL..... That'll teach you, you young welsh pup
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Regular
    Joined
    May 2011
    Posts
    46
    Location
    Quote Originally Posted by Simon Lloyd
    Try using this instead for each one[vba]Sheets("1").Range("B" & Range("B" & Rows.Count).end(xlUp).Row).Value = [/vba] The reason your's currently overwrites each time is because you are taking the value of the last row before adding any values but then the variable isn't updated, so if you had lastrow of 25 it will always remain that until you open the userform again
    Thanks Simon!

  6. #6
    Site Admin VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,005
    Location
    Quote Originally Posted by xld
    What is wrong with Lastrow + 1?

    [vba] With Sheets("1").Range("B" & LastRow)

    .Value = UserForm1.TextBox2.Value
    .Offset(1, 0).Value = UserForm1.TextBox3.Value
    .Offset(2, 0).Value = UserForm1.TextBox4.Value
    End With
    [/vba]
    Hi xld, there was no indication from the OP as to how lastrow was derived or whether it was initiated on call of the userform or within a loop, so it seemed sensible to just give an answer that the OP could implement and understand what's happening
    Quote Originally Posted by Aussiebear
    LOL..... That'll teach you, you young welsh pup
    xld 1, Simon Lloyd 1 i think the score stands at
    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)

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My comment was in reference to what the OP said.
    ____________________________________________
    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

Posting Permissions

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