简体   繁体   中英

Column copy and paste using macro

I am not familiar with VBA or Macro and I use Microsoft excel quite often.

what's better way to write this? What I want is to copy Column B and paste Column B to right next column and every other column.

For example:

on sheet2, I want B column to be on B column

on sheet3, I want B column to be on C column and clear Column B

on sheet4, I want B column to be on E column and clear Column B

on sheet5, I want B column to be on F column and clear Column B

.

.

.

. on sheet 26, I want B column to be on Z column and clear column B

on sheet 27, I want B column to be on AA column and clear column B

so on

I have used " Record Macro" to come up with this but I'd like to use VBA programming. what'd be the best way to copy/cut paste columns?

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

How about:

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

EDIT#1:

To clear column B in each sheet:

   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

EDIT#2

This starts on 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

You can write a Sub to cut down on some of the repetitiveness of your code.

Ex.

Sub copyPaste(sheetNum as integer, copyCol as string, pasteCol as string)

    Sheets(sheetNum).Select
    Columns(copyCol).Copy Columns(pasteCol)
    Columns(copyCol).ClearContents

end sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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