每个循环的MS Excel:插入行 [英] MS Excel For Each Loop: Insert Rows

查看:50
本文介绍了每个循环的MS Excel:插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含242行的工作表.我想在每个现有行下面创建一个新行.相反,我的代码在第1行下方创建了242行.我整个下午都在Google和Stack Overflow上度过,尝试了各种想法,但遇到了同样的问题.这是我的代码:

I have a worksheet containing 242 rows. I want to create a new row beneath each existing one. Instead, my code creates 242 rows below row 1. I have spent all afternoon on Google and Stack Overflow and tried various ideas but get the same problem. Here's my code:

Function rws() As Integer

rws = (Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).row)

End Function

Sub InsRws()

Dim rng As Range
Dim row As Range

Set rng = Range("A1:A" & rws - 1)

For Each row In rng.Rows
    Rows.Select
    ActiveCell.Offset(1).EntireRow.Insert
Next row

End Sub

推荐答案

最简单的方法是像这样递减计数

Easiest way is to count up rather down down, like this

Sub InsertRows()
    Dim rw As Long

    With ActiveSheet
        For rw = .Cells(.Rows.Count, 1).End(xlUp).Row To 2 Step -1
            .Rows(rw).Insert
        Next
    End With
End Sub

这篇关于每个循环的MS Excel:插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆