根据单元格值更改插入行数

时间:2014-01-02 07:45:43

标签: excel vba excel-vba

我真的无法弄清楚这一点。有人可以帮忙吗?

我有两张纸(纸张1和纸张2)。 表单1中有一列记录了所有团队规模 - 团队中有多少人。团队经理可以更改团队规模。例如,昨天团队1的团队规模可以为5,团队经理今天将团队规模改为7。

使用VBA我需要做的是根据团队规模列中的值更改在工作表2中插入行数。使用上面的示例,因为第1组的大小(比如第1页中的单元格E10)从5更改为7,将在工作表2的底部插入两行(7-5 = 2)。

我希望它有道理,我非常感谢任何帮助!非常感谢你。

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效。只需将此代码放在Sheet1模块中:

Dim oldValue As String
'saving old value
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   oldValue = Target.Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
   Application.EnableEvents = False
   'if changing cell in third column
   If Target.Column = 3 Then
      'suppose teamname store in the same row as num of players and in column #2
      teamName = Sheet1.Cells(Target.Row, 2)

      'determining lastrow in sheet2
      lastrow = Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Row

      'insetr changing log
      Sheet2.Range("A" & lastrow + 1) = teamName & ": old value " & oldValue
      Sheet2.Range("A" & lastrow + 2) = teamName & ": new value " & Target.Value
   End If
   Application.EnableEvents = True
End Sub

希望有所帮助

相关问题