查找并 copy/paste 最后一列
Find and copy/paste last columns
我在插入新列并将最后一列的值粘贴到这些新列中时遇到问题。
目前我得到的代码是这样的
Sub Macro1()
Dim ws As Worksheet
Dim LC As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
LC = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC + 1 & ":" & LC + 3).Copy
ws.Range(LC + 1 & "1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
我想在最后三个现有列的前面插入三个新列,并将最后三个现有列的值粘贴到三个新列中。这些区域没有合并单元格。
Copy
和 PasteSpecial
方法都必须在 Range
reference/object 上使用。您正在尝试对 Columns
引用使用 Copy
。试试这个:
Sub Macro1()
Dim ws As Worksheet
Dim lastColumn As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
lastColumn = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(lastColumn - 2).Insert
ws.Columns(lastColumn - 2).Insert
ws.Columns(lastColumn - 2).Insert
ws.Cells(RowIndex:=1, ColumnIndex:=lastColumn + 1).Resize(ColumnSize:=3).EntireColumn.Copy
ws.Cells(RowIndex:=1, ColumnIndex:=lastColumn -2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
您需要小心处理此类代码,因为 lastColumn 的值在您插入列时不会更改,因此可能会产生误导。
试试这个:
Sub insertNew3Col()
Dim ws As Worksheet
Dim LK As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
lc = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Range(Columns(lc - 2), Columns(lc)).Copy
ws.Columns(lc + 1).Insert Shift:=xlToRight
Application.CutCopyMode = False
End Sub
谢谢大家,我不知道这个
Both the Copy and the PasteSpecial methods must be used on a Range >reference/object. You are trying to use Copy on a Columns reference
我按如下方式重新安排了我的代码,结果成功了
Sub Macro1()
Dim ws As Worksheet
Dim LC As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
LC = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Range(Columns(LC + 1), Columns(LC + 3)).Copy
ws.Range(Columns(LC - 2), Columns(LC)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Range(Columns(LC - 2), Columns(LC)).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub
我在插入新列并将最后一列的值粘贴到这些新列中时遇到问题。
目前我得到的代码是这样的
Sub Macro1()
Dim ws As Worksheet
Dim LC As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
LC = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC + 1 & ":" & LC + 3).Copy
ws.Range(LC + 1 & "1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
我想在最后三个现有列的前面插入三个新列,并将最后三个现有列的值粘贴到三个新列中。这些区域没有合并单元格。
Copy
和 PasteSpecial
方法都必须在 Range
reference/object 上使用。您正在尝试对 Columns
引用使用 Copy
。试试这个:
Sub Macro1()
Dim ws As Worksheet
Dim lastColumn As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
lastColumn = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(lastColumn - 2).Insert
ws.Columns(lastColumn - 2).Insert
ws.Columns(lastColumn - 2).Insert
ws.Cells(RowIndex:=1, ColumnIndex:=lastColumn + 1).Resize(ColumnSize:=3).EntireColumn.Copy
ws.Cells(RowIndex:=1, ColumnIndex:=lastColumn -2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
您需要小心处理此类代码,因为 lastColumn 的值在您插入列时不会更改,因此可能会产生误导。
试试这个:
Sub insertNew3Col()
Dim ws As Worksheet
Dim LK As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
lc = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Range(Columns(lc - 2), Columns(lc)).Copy
ws.Columns(lc + 1).Insert Shift:=xlToRight
Application.CutCopyMode = False
End Sub
谢谢大家,我不知道这个
Both the Copy and the PasteSpecial methods must be used on a Range >reference/object. You are trying to use Copy on a Columns reference
我按如下方式重新安排了我的代码,结果成功了
Sub Macro1()
Dim ws As Worksheet
Dim LC As Long
Set ws = ActiveWorkbook.Worksheets("TOT")
LC = ws.Cells(3, Columns.Count).End(xlToLeft).Column
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Columns(LC - 2).Insert
ws.Range(Columns(LC + 1), Columns(LC + 3)).Copy
ws.Range(Columns(LC - 2), Columns(LC)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws.Range(Columns(LC - 2), Columns(LC)).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End Sub