我该如何编码用户窗体以在选定的单元格下方插入指定的行数?

时间:2019-06-25 17:10:48

标签: excel vba userform

我创建了一个用户窗体,该窗体首先询问我要插入多少行。用户表单的下一部分询问在每个新创建的行的第1列和第32列中我想要什么值(我已经对其进行了设置,以便一次最多可以创建6个新行)。我的数据有45列,而我要更改的新创建行中唯一的数据是我之前说过的两列(1和32)中的数据。我希望将原始行中所有其他列的数据复制到每个新行中。我的问题是,我似乎无法弄清楚如何编写可以按照我想要的方式执行此操作的代码。举个例子,如果我响应要在当前活动单元格下方添加3行的用户表单,它将询问我要为这些新行中的每行输入第1列和第32列的值。因此,我将输入以下内容:

第一行
专栏1:8/17/2019
第32栏:400

第二个新行
专栏1:8/10/2019
第32栏:500

第三新行
专栏1:8/3/2019
第32列:600

我尝试了许多不同的代码,但我只真正弄清楚了如何编写它,以便它在活动单元格下面插入一行,并且它完全空白,我不知道如何编程使其进入他为我为第1列和第32列选择的值,并从原始行向下复制所有其他数据。我已经在用户表单上找到了清除和取消按钮的代码,现在我只关心为“确定”按钮编写此代码。

Private Sub CancelButton_Click()

    Unload Me

End Sub

Private Sub ClearButton_Click()

    Call UserForm_Initialize

End Sub

Private Sub OKButton_Click()

    Dim lRow As Long
    Dim lRsp As Long

    On Error Resume Next

    lRow = Selection.Row()
    lRsp = MsgBox("Insert New row above " & lRow & "?", _
            vbQuestion + vbYesNo)
    If lRsp <> vbYes Then Exit Sub

    Rows(lRow).Select
    Selection.Copy
    Rows(lRow + 1).Select
    Selection.Insert Shift:=xlDown

    Application.CutCopyMode = False

    Rows(lRow).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone

End Sub

Private Sub UserForm_Initialize()

    AddRowsTextBox.Value = ""

    Date1TextBox.Value = ""
    Date2TextBox.Value = ""
    Date3TextBox.Value = ""
    Date4TextBox.Value = ""
    Date5TextBox.Value = ""
    Date6TextBox.Value = ""

    Qty1TextBox.Value = ""
    Qty2TextBox.Value = ""
    Qty3TextBox.Value = ""
    Qty4TextBox.Value = ""
    Qty5TextBox.Value = ""
    Qty6TextBox.Value = ""


End Sub

1 个答案:

答案 0 :(得分:0)

我从您的要求中了解到,我将在表单上再添加一个旋转按钮以使其易于使用。可能看起来像这样。

enter image description here

请根据您表单中的控件名称修改用户表单代码

Option Explicit
Public Bal As Double, XQnty As Double, LargeOrder As Double, Sm As Double
Private Sub CommandButton1_Click()
Dim lRow As Long
Dim lRsp As Long

lRow = ActiveCell.Row()
lRsp = MsgBox("Insert New row Below " & lRow & "?", vbQuestion + vbYesNo)


If lRsp <> vbYes Then Exit Sub
Dim Ws As Worksheet
Dim R As Long, i As Integer, RowtoAdd As Integer
Set Ws = ThisWorkbook.ActiveSheet
RowtoAdd = Me.SpinButton1.Value
R = ActiveCell.Row
  With Ws
        .Cells(R, 32).Value = LargeOrder
        For i = 1 To RowtoAdd
        .Cells(R + 1, 1).EntireRow.Insert Shift:=xlDown
        .Cells(R, 1).EntireRow.Copy Destination:=.Cells(R + 1, 1)
        .Cells(R + 1, 1).Value = Me.Controls("TextBox" & i).Value
        .Cells(R + 1, 32).Value = Me.Controls("TextBox" & 7 + i).Value
        R = R + 1
        Next i
  End With
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub SpinButton1_Change()
Dim x As Integer, i As Integer, Y As Double
Me.TextBox7.Value = Me.SpinButton1.Value
x = Me.SpinButton1.Value
Sm = 0
    For i = 1 To 6
    Me.Controls("TextBox" & i).BackColor = IIf(i <= x, RGB(255, 100, 100), RGB(255, 2550, 255))
    Me.Controls("TextBox" & i + 7).Value = IIf(i <= x, Int(Bal / x), 0)
    Sm = Sm + IIf(i <= x, Int(Bal / x), 0)
    Next

    If Sm <> Bal Then
    Me.TextBox8.Value = Int(Bal / x) + Bal - Sm
    End If
ManualBal
End Sub
Private Sub TB_LO_Change()
LargeOrder = Val(Me.TB_LO.Value)
Bal = XQnty - LargeOrder
ManualBal
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer, dx As Variant
Me.SpinButton1.Value = 1
Me.TextBox7.Value = 1
Me.TextBox1.BackColor = RGB(255, 100, 100)
dx = ThisWorkbook.ActiveSheet.Cells(ActiveCell.Row, 1).Value
XQnty = ThisWorkbook.ActiveSheet.Cells(ActiveCell.Row, 32).Value
LargeOrder = 575
Bal = XQnty - LargeOrder
Sm = 0
If IsDate(dx) = False Then dx = Now()
    For i = 1 To 6
    Me.Controls("TextBox" & i).Value = Format(dx - i * 7, "mm-dd-yyyy")
    Sm = Sm + Int(Bal / 6)
    Me.Controls("TextBox" & i + 7).Value = Int(Bal / 6)
    Next
    If Sm <> Bal Then
    Me.TextBox8.Value = Int(Bal / 6) + Bal - Sm
    End If
Me.TB_LO = LargeOrder
Me.TB_Bal = 0
End Sub
Private Sub ManualBal()
Dim x As Integer, i As Integer
x = Me.SpinButton1.Value
Bal = XQnty - LargeOrder
Sm = 0
    For i = 1 To 6  ' Or may use 6 insted of X
    Sm = Sm + Val(Me.Controls("TextBox" & i + 7).Value)
    Next
    Me.TB_Bal.Value = Bal - Sm
End Sub
Private Sub TextBox8_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub
Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub
Private Sub TextBox10_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub
Private Sub TextBox11_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub
Private Sub TextBox12_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub
Private Sub TextBox13_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ManualBal
End Sub

在此处文本框1至6用于日期,在7中为Spin Button值,在文本框8至13中用于数量。可以根据控件名称修改代码,也可以根据代码修改控件名称。

编辑:添加了两个名为TB_BAL的新文本框,以在在“数量”文本框中手动输入值时显示(仅在文本框退出事件时才计算余额),而TB_LO则更改{{1} }。