使用文本框(V​​BA)的连续编号

时间:2019-05-04 04:55:21

标签: excel vba

enter image description here

我让文本框将orden赋予另一个文本框的问题是连续数字。例子

这是我的用户表单

(Textbox1 ) (TextBox2)
Product:     Number:
Door car    4

这是我的excel表格:

Product:    Number:
Door Car    1
Door Car    2
Door Car    3
Door Car    4

谢谢,抱歉我的英语。

1 个答案:

答案 0 :(得分:0)

用户关注代码

Private Sub CommandButton1_Click()
Dim sht As Worksheet
Dim i As Long

Set sht = Worksheets("Sheet1")

    If Not IsNull(Me.TextBox2) Then
        For i = 1 To Val(Me.TextBox2)
            sht.Cells(i + 1, 1) = Me.TextBox1
            sht.Cells(i + 1, 2) = i
        Next i
    End If

Set sht = Nothing
End Sub
  

编辑:对于最后一行,请尝试以下代码。

Private Sub CommandButton1_Click()
Dim sht As Worksheet
Dim lasRow As Long
Dim i As Long

Set sht = Worksheets("Sheet1")
lasRow = sht.Cells(Rows.Count, "E").End(xlUp).Offset(1)

    If Not IsNull(Me.TextBox4) Then
        For i = 1 To Val(Me.TextBox4)
            sht.Cells(Rows.Count, "E").End(xlUp).Offset(1) = Me.TextBox4
            sht.Cells(Rows.Count, "E").End(xlUp).Offset(0, 1) = Me.ComboBox5
        Next i
    End If

Set sht = Nothing
End Sub