简体   繁体   English

插入一系列单元格,而不是插入整行

[英]Insert a range of cells instead of inserting entire row

I have the following code: 我有以下代码:

If strSheetDate < strFileNameDate Then
    With WS1
        .Rows(2).Insert Shift:=xlDown
        .Cells(2, 1).Value = strFileNameDate
        .Cells(2, 2).Value = WS3.Cells(2, 2).Value
        .Cells(2, 3).Value = .Cells(1, 11).Value
        .Cells(2, 4).Value = WS3.Cells(3, 2).Value
        .Cells(2, 5).Value = .Cells(1, 13).Value
    End With      
End If

and i would like to swap the line: 我想换行:

.Rows(2).Insert Shift:=xlDown

to something where it only moves columns A to E down as I'd like to use the rest of the sheet for more data but don't want it to move down everytime this code is run. 到只将A到E列向下移动的东西,因为我想使用工作表的其余部分来获取更多数据,但不希望每次运行此代码时都将其向下移动。

cheers 干杯

EDIT Solution : 编辑解决方案

Thanks to the 2 users below I now have the following 2 sets of code, this one is for a simple insert: 感谢下面的2个用户,我现在有以下2组代码,该组代码用于简单的插入:

If strSheetDate < strFileNameDate Then

    With WS1
        .Range("A2:E2").Insert shift:=xlDown
        .Cells(2, 1).Value = strFileNameDate            
        .Cells(2, 2).Value = WS3.Cells(2, 2).Value
        .Cells(2, 3).Value = lngAuthormax               
        .Cells(2, 4).Value = WS3.Cells(3, 2).Value
        .Cells(2, 5).Value = lngConsumermax             
    End With
End If

and the second is inserting off of a cell in a range: 第二个是插入一个范围内的单元格:

iRow2 = WS1.Cells(Rows.Count, 1).End(xlUp).Row
Set rngSheetDate2 = WS1.Range("A2:A" & iRow2)

For Each cell In rngSheetDate2
    If cell.Value > strFileNameDate And cell.Offset(1, 0).Value < strFileNameDate Then
        WS1.Range(cell.Offset(1, 0), cell.Offset(1, 4)).Insert Shift:=xlDown, _
        CopyOrigin:=xlFormatFromLeftOrAbove
        cell.Offset(1, 0).Value = strFileNameDate
        cell.Offset(1, 1).Value = WS3.Cells(2, 2).Value
        cell.Offset(1, 2).Value = lngAuthormax
        cell.Offset(1, 3).Value = WS3.Cells(3, 2).Value
        cell.Offset(1, 4).Value = lngConsumermax
        Exit For
    End If
Next

Thank you 谢谢

尝试这个:

.Range(.cells(2, 1), .cells(2, 5)).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM