Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 复制最后两列并插入到其左侧_Vba_Excel - Fatal编程技术网

Vba 复制最后两列并插入到其左侧

Vba 复制最后两列并插入到其左侧,vba,excel,Vba,Excel,我很难复制工作表中最后两列的数据,然后在其左侧插入相同的列。这是到目前为止我的代码,它复制了正确的列,但只是将它们粘贴到右侧 Sub GlobalPerformNewMonths() Dim lngLastRow As Long With ThisWorkbook.Worksheets("Global Supplier Performance") lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row .C

我很难复制工作表中最后两列的数据,然后在其左侧插入相同的列。这是到目前为止我的代码,它复制了正确的列,但只是将它们粘贴到右侧

Sub GlobalPerformNewMonths()

Dim lngLastRow     As Long

With ThisWorkbook.Worksheets("Global Supplier Performance")
     lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
     .Cells(1, .Columns.Count).End(xlToLeft).Offset(,Resize(lngLastRow,Copy .Cells(1, .Columns.Count).End(xlToLeft).Offset(, -2)
     Columns("D:AZ").ColumnWidth = 18.43
     Application.CutCopyMode = False
End With
End Sub

尝试选择所需数据的整列。选择它们,然后将它们插入到所需列的左侧

Range("d:e").Select
Selection.Copy
Columns("C:C").Select
Selection.Insert Shift:=xlToRight

您必须从找到的最后一列转移到适当的单元格。为此,我建议在使用数字特定寻址的地方使用

Sub GlobalPerformNewMonths()
    Dim lr As Long, lc As Long

    With ThisWorkbook.Worksheets("Global Supplier Performance")
         lr = .Cells(Rows.Count, 1).End(xlUp).Row
         lc = .Cells(1, Columns.Count).End(xlToLeft).Column
         With .Cells(1, lc - 1).Resize(lr, 2)
            .Copy
            .Insert Shift:=xlToRight
         End With
         .Columns("D:AZ").ColumnWidth = 18.43
         Application.CutCopyMode = False
    End With
End Sub