Entering Data into a spreadsheet through a UserFor

2019-09-13 05:34发布

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

1条回答
Summer. ? 凉城
2楼-- · 2019-09-13 06:13

This line here is wrong:

lngWriteRow = ws.Cells(Rows.Count, 12) _
.End(xlUp).Offset(1, 0).Row

Because you are referring to column 12, which you do not alter - hence the row stays the same.

Use this instead

lngWriteRow = ws.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row

Edit:

If you want an initial offset, to start the data-input @ row 13, use this:

lngWriteRow = ws.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row

if lngWriteRow < 13 then lngWriteRow = 13

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.

Private Sub Test()

Dim lngWriteRow As Long

Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

lngWriteRow = ws.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row

If lngWriteRow < 13 Then lngWriteRow = 13

    ws.Range("B" & lngWriteRow) = "test"
    ws.Range("C" & lngWriteRow) = "test"
    ws.Range("D" & lngWriteRow) = "test"
    ws.Range("E" & lngWriteRow) = "test"
    ws.Range("F" & lngWriteRow) = "test"
    ws.Range("G" & lngWriteRow) = "test"

End Sub

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

title-row
row13
row..
row..
row63
space
other stuff

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:

lngWriteRow = ws.Cells(ws.Range("B12:B63")Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row

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.

查看更多
登录 后发表回答