我已经将这段代码放在一个属于UserForm的按钮上......
Private Sub CommandButton1_Click()
Dim lngWriteRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
NextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
lngWriteRow = ws.Cells(Rows.Count, 2).End(xlUp) _
.Offset(12, 0).NextRow
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
它不会自动移动到下一行,并且会覆盖行号14,当我希望它更新15时14中有数据,然后当15,更新16等等......
有什么想法吗?
答案 0 :(得分:1)
由于您只是将数据放在B列以上,因此您应该查看这些列,而不是代码当前正在执行的A列。这应该解决它:
lngWriteRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(12, 0).Row + 1
答案 1 :(得分:0)
虽然约翰似乎已经触头,但我认为你可以像这样进一步简化你的代码。请参阅指定lngWriteRow
的地方的评论:
Private Sub CommandButton1_Click()
Dim lngWriteRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ws
'-> if you are going to offset 12 rows then add 1 more, just offset to 13 and be done!
lngWriteRow = .Cells(.Rows.Count, 2).End(xlUp).Offset(13, 0).Row
.Range("B" & lngWriteRow) = TextBox1.Value
.Range("C" & lngWriteRow) = TextBox2.Value
.Range("D" & lngWriteRow) = TextBox3.Value
.Range("E" & lngWriteRow) = ComboBox1.Value
.Range("F" & lngWriteRow) = TextBox4.Value
.Range("G" & lngWriteRow) = ComboBox2.Value
End With
End Sub