如何编写我的用户表单以在所选单元格下方插入指定行数?
How do I code my userform to insert the specified number of rows beneath the selected cell?
我创建了一个用户表单,它首先询问我要插入多少行。用户窗体的下一部分询问我希望在每个新创建的行的第 1 列和第 32 列中有什么值(我已将其设置为一次最多可以创建 6 个新行)。我的数据有 45 列,我想在新创建的行中更改的唯一数据是我之前所说的两列(1 和 32)中的数据。我希望将原始行中所有其他列的数据复制到每个新行中。我的问题是我似乎无法弄清楚如何编写代码来按照我想要的方式执行此操作。举个例子,如果我回应我想在当前活动单元格下方添加 3 行的用户表单,它会询问我想为这些新行中的每一行的第 1 列和第 32 列输入什么值。所以我会输入这样的内容:
第一个新行
第 1 列:2019 年 8 月 17 日
第 32 列:400
第二个新行
第 1 列:2019 年 8 月 10 日
第 32 列:500
第三个新行
第 1 列:2019 年 8 月 3 日
第 32 列:600
我已经尝试了很多不同的代码,但我只是真正想出了如何编写它以便它在活动单元格下方插入一行并且它完全空白,我不知道如何编程它以便它输入我为第 1 列和第 32 列选择的值,并从原始行向下复制所有其他数据。我已经在我的用户窗体上找到了清除和取消按钮的代码,我现在只关心为 "OK" 按钮编写这段代码。
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
据我了解,根据您的要求,我会在表单上再添加一个旋转按钮,以使其更易于使用。它可能看起来像这样。
用户表单代码请根据您表单中的控件名称进行修改
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 用于微调按钮值,文本框 8 到 13 用于数量。请根据控件名称修改代码或根据代码修改控件名称。
编辑:添加了两个名为 TB_BAL
的新文本框,用于在数量文本框中手动输入值时显示(仅在文本框退出事件时计算余额)和 TB_LO
以更改 LargeOrder
在 运行 期间。
我创建了一个用户表单,它首先询问我要插入多少行。用户窗体的下一部分询问我希望在每个新创建的行的第 1 列和第 32 列中有什么值(我已将其设置为一次最多可以创建 6 个新行)。我的数据有 45 列,我想在新创建的行中更改的唯一数据是我之前所说的两列(1 和 32)中的数据。我希望将原始行中所有其他列的数据复制到每个新行中。我的问题是我似乎无法弄清楚如何编写代码来按照我想要的方式执行此操作。举个例子,如果我回应我想在当前活动单元格下方添加 3 行的用户表单,它会询问我想为这些新行中的每一行的第 1 列和第 32 列输入什么值。所以我会输入这样的内容:
第一个新行
第 1 列:2019 年 8 月 17 日
第 32 列:400
第二个新行
第 1 列:2019 年 8 月 10 日
第 32 列:500
第三个新行
第 1 列:2019 年 8 月 3 日
第 32 列:600
我已经尝试了很多不同的代码,但我只是真正想出了如何编写它以便它在活动单元格下方插入一行并且它完全空白,我不知道如何编程它以便它输入我为第 1 列和第 32 列选择的值,并从原始行向下复制所有其他数据。我已经在我的用户窗体上找到了清除和取消按钮的代码,我现在只关心为 "OK" 按钮编写这段代码。
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
据我了解,根据您的要求,我会在表单上再添加一个旋转按钮,以使其更易于使用。它可能看起来像这样。
用户表单代码请根据您表单中的控件名称进行修改
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 用于微调按钮值,文本框 8 到 13 用于数量。请根据控件名称修改代码或根据代码修改控件名称。
编辑:添加了两个名为 TB_BAL
的新文本框,用于在数量文本框中手动输入值时显示(仅在文本框退出事件时计算余额)和 TB_LO
以更改 LargeOrder
在 运行 期间。