Vba 从选定值插入行

Vba 从选定值插入行,vba,excel,Vba,Excel,我有两列A和B A = 12 (months) B = 12.000.000 C (result) = B/A = 12.000.000 / 12 = 1.000.000 我想要的结果将是基于月份(A)自动循环插入新行 这是我在案例中添加时的完整代码: Private Sub cmdAdd_Click() Dim lRow As Long Dim lPart As Long Dim ws As Worksheet Set ws = Worksheets("PostBudgetData") l

我有两列A和B

A = 12 (months)
B = 12.000.000
C (result) = B/A = 12.000.000 / 12 = 1.000.000
我想要的结果将是基于月份(A)自动循环插入新行

这是我在案例中添加时的完整代码:

Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("PostBudgetData")

lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

lPart = Me.cboMonth.ListIndex
    If CheckBox1.Value = True Then
    If Trim(Me.cboMonth.Value) = "" Then
        Me.cboMonth.SetFocus
        MsgBox "Please enter a part number"
    Exit Sub
    End If
End If



'copy the data to the database
With ws
  .Cells(lRow, 1).Value = Me.cboYear.Value
If CheckBox1.Value = True Then
      .Cells(lRow, 2).Value = Me.cboMonth.List(lPart, 0)
End If
  .Cells(lRow, 3).Value = Me.cboBrand.Value
  .Cells(lRow, 4).Value = Me.cboPostBudget.Value
  .Cells(lRow, 5).Value = Me.cboArea.Value
  .Cells(lRow, 6).Value = Me.txtValue.Value
  .Cells(lRow, 7).Value = Me.cboSBU.Value
End With

'clear the data
Me.cboMonth.Value = ""
Me.cboYear.Value = ""
Me.cboBrand.Value = ""
Me.cboPostBudget.Value = ""
Me.cboArea.Value = ""
Me.cboSBU.Value = ""

'Me.txtValue.Value = Format(Date, "Medium Date")
Me.txtValue.Value = 0

If CheckBox1.Value = True Then
      Me.cboMonth.SetFocus
End If

End Sub

但这不是循环。我不知道在插入新行时循环vba,因为它只插入一行。这是我尝试过的,不仅仅是我累了,什么都不做就问

此VBA代码从单元格
A2
B2
获取值,并在列
D
E
上生成新行:

Private Sub Worksheet_Change(ByVal Target As Range)
    first_column = 4 'first column of results
    total_months = Cells(2, 1) 'cell of total months
    total_amount = Cells(2, 2) 'cell of total amount
    calculated_value = total_amount / total_months
    changed = False
    Dim a As Range
    Select Case Target.Address
        Case "$A$2"
            changed = True
        Case "$B$2"
            changed = True
    End Select
    If changed = True Then 'only updates if change cells A2 or B2
        Columns(first_column).ClearContents
        Columns(first_column + 1).ClearContents
        Cells(1, first_column) = "month"
        Cells(1, first_column + 1) = "amount"
        For i = 2 To total_months + 1 ' loop the number of rows=totalmonths
            Cells(i, first_column) = i - 1
            Cells(i, first_column + 1) = calculated_value
        Next i
    End If
End Sub
您必须复制代码,进入Excel中的查看->宏,创建一个新的(任何名称都有效),然后在左列双击要使用它的工作表(图片中的红旗),然后在右侧粘贴代码

这就是它看起来的样子:


如果您只想了解如何在vba中循环,那么这可能会帮助您:

'12 = times you want to loop
For i = 1 to 12 

'inserts row before row number i
ThisWorkbook.Sheets(1).Rows(i).Insert
'fill cells with content
Sheets(1).Cells(i,1) = <something

'loop 
Next i
'12=要循环的次数
对于i=1到12
'在第i行之前插入行
此工作簿。工作表(1)。行(i)。插入
'用内容填充单元格

Sheets(1).Cells(i,1)=在这里获得帮助的最佳方式是首先尝试一些东西。如果你陷入困境,做一些研究,尝试自己解决问题,然后再问一个关于你的尝试的具体问题,展示你的尝试。在没有证明研究成果的情况下,要求完整解决方案的问题通常会被否决和关闭。我只想添加一个(Excel表)[它们会触发事件,并在任何时候输入一个新值时下拉您的公式,而不会产生VBA(阻止等)的不良副作用。处理非共享工作簿。非常感谢您的帮助:)
'12 = times you want to loop
For i = 1 to 12 

'inserts row before row number i
ThisWorkbook.Sheets(1).Rows(i).Insert
'fill cells with content
Sheets(1).Cells(i,1) = <something

'loop 
Next i