I am new to VBA in Excel, and I have a basic userform which is to place the data into the sheet. the data from the form is to enter in cell B13 through to G13, then every other entry after should be done on the next row down e.g. B14-G14.
I have this code already however it isnt entering the data into the correct cell and is repeatedly entering it on the same row...
Private Sub CommandButton1_Click()
Dim lngWriteRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lngWriteRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If lngWriteRow < 13 Then lngWriteRow = 13
ws.Range("B" & lngWriteRow) = TextBox1.Value
ws.Range("C" & lngWriteRow) = TextBox2.Value
ws.Range("D" & lngWriteRow) = TextBox3.Value
ws.Range("E" & lngWriteRow) = ComboBox1.Value
ws.Range("F" & lngWriteRow) = TextBox4.Value
ws.Range("G" & lngWriteRow) = ComboBox2.Value
End Sub
How would i achieve this? (There is already data on the rows below)
Thanks in advance
This line here is wrong:
Because you are referring to column 12, which you do not alter - hence the row stays the same.
Use this instead
Edit:
If you want an initial offset, to start the data-input @ row 13, use this:
You cannot use
Offset(12,0)
, because you would use it everytime!Edit
Just to be crystal clear, this here works on an empty sheet, when pasting the code as a worksheet-macro and hitting F5 multiple times. So, unless there is explained, what this does wrong, I consider the question solved.
Edit
After some mailing, here is the solution to this riddle: it was not stated, that there are filled cells beneath those, which shall be entered.
So for col-B it was more like
basically the suggested corrections worked - but they looked for the last filled cell in column B on the whole sheet, which was the problem.
Here is the solution to that:
And to give you some explanaition on the way:
You can't use
(Rows.Count,1)
instead of(Rows.Count,2)
, because you are adding Data in the columns B-G, which is 2-7. You have to use 2-7 because of the way, you are looking for the last row. If you use 1, you're looking for the last value in column A, which does not change, when you are trying to add new data.You can't use
Offset(12,0)
, because this would create an offset everytime you insert data - so you would end up with rows 12 rows apart.And finally, you can't use
Rows.Count
, because this is 65536 or so, and you have data beneath the data you are adding.End(xlUp)
will lookup from too far down, and stop at the last cell of column B, which has data in it - but this won't be B13, unless there is no data in B14-B65536.Hope this helps to understand the dynamics here.