我尝试做的是使用VBA在Excel范围的特定位置插入一行。我扫描范围,找出字符串"管理"首先出现,然后想直接在上面插入一行。以下代码有效。但是它只向我的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
答案 0 :(得分:0)
而不是
target.Rows(insertRowNum).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
试
Range("A" & insertRowNum).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove