引用Userform

时间:2016-01-20 14:28:28

标签: vba userform

所以我创建了一个userform,最终我希望能够根据用户在userform中输入的数字添加行。我有代码来插入行,但我不知道如何将它与用户表单以及用户输入的内容相结合。我现在的代码是:

Public Sub Insert()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual   'pre XL97 xlManual

    lastrow = ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.Cells(lastrow, 1).Select

    Set CurrentCell = ActiveSheet.Cells(lastrow, 1)

    For n = lastrow To 0 Step -1
        If n = lastrow Then GoTo CheckLastRow
        If n = 1 Then GoTo CheckfirstRow
            ActiveCell.Offset(-2, 0).Select

CheckLastRow:

Set NextCell = CurrentCell.Offset(-1, 0)

            ActiveCell.Offset(1, 0).Select
            For i = 1 To CurrentCell
                ActiveCell.EntireRow.Insert
            Next i

        Set CurrentCell = NextCell
    Next n

    'To be performed on the firstrow in the column
CheckfirstRow:
            ActiveCell.Offset(-1, 0).Select
            For i = 1 To CurrentCell
                ActiveCell.EntireRow.Insert
            Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:1)

如果您只是从用户那里获取行数。我建议使用InputBox。如果您正在查看其他内容并且只想使用UserForms。试试下面的内容,看看它是否有效。

'Procedure that triggers the user Form
Sub InitiateUserFor()
 UserForm1.Show
End Sub
 'On Click on Button on UserForm
 Private Sub CommandButton1_Click()
 Call Insert()
 End Sub

在Insert()中添加一个变量以从UserForm中读取值

 Dim RowsToInster as Integer
 RowsToInster =UserForm.TextBox1.Value