2016-01-20 53 views
0

所以我做了一个用户表单,最终我希望能够根据用户键入到用户表单中的数字添加行。我的代码插入行,但我不知道如何将它与用户窗体和什么人类型的联合,我现在的代码是:引用用户表单

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

读值你的问题和代码是有点不清楚......究竟是什么你想完成什么?您希望用户输入一个数字,然后插入许多新的行,但您希望插入的位置和次数? – FredGooch

+0

如果用户输入5,我想插入5行。位置在选定的单元格下。谢谢你的时间 –

+0

好吧,你的意思是这是一个单独的宏吗?你的'Insert()'代码不会做你刚才描述的内容...... – FredGooch

回答

1

如果你只看着服用来自用户的行数。我会建议使用InputBox。如果您正在查看其他内容并希望仅使用用户窗体进行操作。尝试下面的内容,看看它是否有效。

'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()从用户窗体

Dim RowsToInster as Integer 
RowsToInster =UserForm.TextBox1.Value