PDA

View Full Version : Macro Code Overwrites Previous Data



Ethan
05-04-2012, 11:44 AM
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"?



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

Simon Lloyd
05-04-2012, 01:31 PM
Try using this instead for each oneSheets("1").Range("B" & Range("B" & Rows.Count).end(xlUp).Row).Value = 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 :)

Bob Phillips
05-04-2012, 04:31 PM
What is wrong with Lastrow + 1?

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

Aussiebear
05-04-2012, 08:20 PM
LOL..... That'll teach you, you young welsh pup :devil2:

Ethan
05-04-2012, 11:33 PM
Try using this instead for each oneSheets("1").Range("B" & Range("B" & Rows.Count).end(xlUp).Row).Value = 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!

Simon Lloyd
05-05-2012, 02:44 PM
What is wrong with Lastrow + 1?

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
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 :)
LOL..... That'll teach you, you young welsh pup :devil2:xld 1, Simon Lloyd 1 i think the score stands at ;)

Bob Phillips
05-05-2012, 04:28 PM
My comment was in reference to what the OP said.