我不熟悉VBA或Macro,我经常使用Microsoft excel。
有什么更好的方式来写这个?我想要的是复制B列并将B列粘贴到右下一列和每隔一列。
例如:
在sheet2上的,我希望B列在B列上
在sheet3上,我希望B列位于C列并清除B列
在sheet4上,我希望B列在E列上并清除B列
在sheet5上,我希望B列在F列上并清除B列
。 在表26中,我希望B列在Z列上并清除B列
在表27上的,我希望B列在AA列上并清除B列
等等
我用过"录制宏"想出这个,但我想使用VBA编程。什么是复制/剪切粘贴列的最佳方法?
Sheets(2).Select
Columns("B:B").Select
Sheets(3).Select
Columns("B:B").Select
Selection.Copy
Columns("C:C").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(4).Select
Columns("B:B").Select
Selection.Copy
Columns("D:D").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(5).Select
Columns("B:B").Select
Selection.Copy
Columns("E:E").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(6).Select
Columns("B:B").Select
Selection.Copy
Columns("F:F").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(7).Select
Columns("B:B").Select
Selection.Copy
Columns("G:G").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(8).Select
Columns("B:B").Select
Selection.Copy
Columns("H:H").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
Sheets(9).Select
Columns("B:B").Select
Selection.Copy
Columns("I:I").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("B:B").Select
Selection.ClearContents
答案 0 :(得分:1)
怎么样:
Sub ytrewq()
Dim i As Long
For i = 1 To Sheets.Count
With Sheets(i)
.Columns(2).Copy .Columns(i + 2)
End With
Next i
End Sub
修改#1:强>
在每张表格中清除列 B :
Sub ytrewq()
Dim i As Long
For i = 1 To Sheets.Count
With Sheets(i)
.Columns(2).Copy .Columns(i + 2)
.Columns(2).Clear
End With
Next i
End Sub
修改#2 强>
这从 Sheet3 开始:
Sub ytrewq()
Dim i As Long
For i = 3 To Sheets.Count
With Sheets(i)
.Columns(2).Copy .Columns(i)
.Columns(2).Clear
End With
Next i
End Sub
答案 1 :(得分:0)
您可以编写一个Sub来减少代码的某些重复性。
实施例
Sub copyPaste(sheetNum as integer, copyCol as string, pasteCol as string)
Sheets(sheetNum).Select
Columns(copyCol).Copy Columns(pasteCol)
Columns(copyCol).ClearContents
end sub