【发布时间】:2014-09-21 01:10:00
【问题描述】:
我要做的是使用 VBA 在 Excel 范围内的特定位置插入一行。我扫描范围以找出字符串“Administration”首先出现在哪一行,然后想在上面直接插入一行。下面的代码有效。但它只会在我的 Range 中插入一行,而不是在整个工作表中插入一行,这会弄乱同一个工作表上其他一些表的位置。找出一种选择整行的方法会很好,这样工作表上的所有内容也会重新定位,而不仅仅是范围内的内容。有什么想法吗?谢谢。
Sub insertRow(Name As String)
Dim Rw, target As Range
Dim insertRowNum As Integer
Set target = ActiveSheet.Range("Table")
'Determine proper insertion row index. Goal is to insesrt new project right
'before Administration'
insertRowNum = -1 'Flag to see if loop finds "Administration" in any of the cells"
For Each Rw In target.Rows
If target.Cells(Rw.Row, 1) = "Administration" Then
insertRowNum = Rw.Row
Exit For
End If
Next
'Check to see if "Administration" was found'
If insertRowNum = -1 Then
insertRowNum = target.Rows.Count
End If
'Insert Blank Row
target.Rows(insertRowNum).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
【问题讨论】: