0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel VBA 从某一指定行开始,循环插入空白行,并向其中插入数据

Posted at

从第二行开始,向下按照B列的值循环插入数据
如果符合条件1,或者条件2,则将数据集合1s的数据逐个插入下面的单元格中,
如果符合条件2,或者条件3,或者条件4,则将数据集合2s的数据逐个插入下面的单元格中

Sub 根据条件循环插入数据()
    i = 2
    数据集合1s = Array("数据1-1", "数据1-2", "数据1-3", "数据1-4")
    数据集合2s = Array("数据2-1", "数据2-2", "数据2-3", "数据2-4", "数据2-5", "数据2-6", "数据2-7")
    Do
        Select Case Cells(i, "B")
            Case "条件1", "条件2"
                For Each 数据集合1 In 数据集合1s
                    Rows(i + 1).Insert shift:=xlShiftDown
                    Cells(i + 1, "C") = Cells(i, "C")
                    Cells(i + 1, "B") = Cells(i, "B")
                    Cells(i, "A") = 数据集合1
                    i = i + 1
                Next 数据集合1
                Rows(i).Delete
                
            Case "条件1", "条件2","条件3"
                For Each 数据集合2 In 数据集合2s
                    Rows(i + 1).Insert shift:=xlShiftDown
                    Cells(i + 1, "C") = Cells(i, "C")
                    Cells(i + 1, "B") = Cells(i, "B")
                    Cells(i, "A") = 数据集合2
                    i = i + 1
                Next 数据集合2
                Rows(i).Delete
                
        End Select
        Debug.Print i
        If i Mod 2000 = 0 Then
            Debug.Print i
        End If
    Loop While Cells(i, "C") <> ""
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?