VBA复制一定范围的单元格并在特定偏移量之后粘贴它们

问题描述 投票:0回答:1

我正在尝试复制一定范围的单元格并将其粘贴到同一工作表中,但要保留几列。我收到应用程序定义的错误。是什么赋予了?我遵循了此示例here

    MsgBox "Would you like to save the chart data for the completed sprint?", vbOKCancel
    If answer = 0 Then
        MsgBox "Copying ..."

        Worksheets("KPIs").Range("AN:AY").Copy
        Worksheets("KPIs").Range("AN:AY").Offset(3).Insert Shift:=xlRight
        Application.CutCopyMode = False

    Else
        End
    End If

UPDATE

将offset(3)修改为offset(0,3)之后,我遇到了一个无法完全显示的新错误(Excel到底是什么?)

enter image description here

* UPDATE 1/17 *

Sub d_KPIsDisplay()    
If StrComp(Trim(Worksheets("KPIs").Cells(1, 39)), Trim(Left(Worksheets("KPIs").Range("G2"), 7))) = 0 Or Len(Trim(Worksheets("KPIs").Cells(1, 39))) = 0 Then
    If MsgBox("Click OK to display charts!", vbOKCancel) = vbOK Then
        Worksheets("KPIs").Range("AN:AY").Clear
        Worksheets("KPIs").Range("AN:AY").Borders.LineStyle = xlNone
        Worksheets("KPIs").Range("AN:AY").Interior.Color = xlNone
        Set fSo = New FileSystemObject
        With New FileSystemObject
            If .FileExists("C:\Project Files\KPIs_log.txt") Then
               .DeleteFile "C:\Project Files\KPIs_log.txt"
            End If
        End With
        Set logFile = fSo.CreateTextFile("C:\Project Files\KPIs_log.txt", 8, True)
        KPI_Velocity
        KPI_TaskProgress
        Worksheets("KPIs").Cells.EntireColumn.AutoFit
        MsgBox "Done", vbExclamation
        End
    Else
        End
    End If
Else
    If MsgBox("Would you like to save the chart data for the completed sprint?", vbOKCancel) = vbOK Then
        MsgBox "Copying ..."

        Worksheets("KPIs").Range("AN:AY").Cut
'            Worksheets("KPIs").Range("AY:AY").Offset(, 15).Insert Shift:=xlRight


        With Worksheets("KPIs").Range("AN:AY")
            .Offset(, .Columns.Count + 14).Insert Shift:=xlShiftToRight
        End With
        Application.CutCopyMode = False
    Else
        End
    End If
End If

结束子

excel vba
1个回答
0
投票

Offset的参数是

也是xlShiftToRight,而不是xlRight

正如评论中已经提到的,您可以使用范围的大小并添加3列; With块是有帮助的:

With Worksheets("KPIs").Range("AN:AY")
    .Offset(, .Columns.Count + 3).Insert Shift:=xlShiftToRight
End With
© www.soinside.com 2019 - 2024. All rights reserved.