如何使用Userform从特定行(例如A8)值开始捕获数据

时间:2018-09-01 18:53:56

标签: excel vba excel-vba

Desired Output should start from A8 row我要开始从特定行范围(例如A8)捕获数据,并继续到下一行。在我的代码中,选择范围时出现错误,任何人都可以指导我解决此错误。

下面是附带的代码:

Private Sub cmdadd_Click()
    Dim StockNumber As Long, LastRow As Long
    Dim wsStockSheet As Worksheet
    Set wsStockSheet = ThisWorkbook.Worksheets("Sheet1")
    StockNumber = Application.Max(wsStockSheet.Columns(1)) + 1

    With wsStockSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            .Range("A8").Select
            .Range("A" & LastRow).Value = StockNumber
            .Range("B" & LastRow).Value = Me.cbodd.Text
            .Range("C" & LastRow).Value = Me.txtPro.Text
            .Range("D" & LastRow).Value = Me.txtEn.Text
        End With    
 End Sub

1 个答案:

答案 0 :(得分:0)

您自己的代码已注释。

Private Sub cmdadd_Click() 'each time you press button cmadd
    Dim StockNumber As Long, LastRow As Long
    Dim wsStockSheet As Worksheet
    Set wsStockSheet = ThisWorkbook.Worksheets("Sheet1")
    StockNumber = Application.Max(wsStockSheet.Columns(1)) + 1 'Find the maximun value in column A. Then add one.

    With wsStockSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 'Find last row. Then add one (i.e., the row following last with data)
        If (LastRow < 7) Then LastRow = 8 'If last row with data is less than 8, then make it be 8
            'in columns A, B, C and D of that row, write the corresponding textbox data
            .Range("A" & LastRow).Value = StockNumber
            .Range("B" & LastRow).Value = Me.cbodd.Text
            .Range("C" & LastRow).Value = Me.txtPro.Text
            .Range("D" & LastRow).Value = Me.txtEn.Text
        End With
 End Sub