Copy a specific column in a different sheet and when you run the macro it moves columns to the right

StackOverflow https://stackoverflow.com/questions/22716073

  •  23-06-2023
  •  | 
  •  

Pregunta

i need some help as i am experienceing a problem, pretty new to VBA. i want to copy that data from L5-L18, excluding some cells and paste it to column B of sheet(In) and create a button that every time i push it to copy the data from column B ,sheet(Data) to the sheet(in) and move columnto the right. like first time column b, next time column c...every time i push the button.. much appreciated

Sub Macro2()
 Sheets("Data").Select

 Range("L5,L6,L7,L8,L9,L10,L13,L14,L15,L16,L17,L18").Select

 Range("L18").Activate

Selection.Copy

 Sheets("In").Select

 Range("B5").Select

ActiveSheet.Paste

 Range("B5").Offset(0, 1).Select

End Sub
¿Fue útil?

Solución

Method A

To insert into column B and shift everything else to the right try this:

Sub offsetCol()
    Dim wksData As Worksheet
    Set wksData = Sheets("Data")

    Dim wksIn As Worksheet
    Set wksIn = Sheets("In")

    Application.CutCopyMode = False
    Columns("B:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    wksIn.Range("B5:B10").Value = wksData.Range("L5:L10").Value
    wksIn.Range("B11:B16").Value = wksData.Range("L13:L18").Value
End Sub

Method B

Find last column in sheet and tack on information to next available:

Sub offsetCol()
    Dim wksData As Worksheet
    Set wksData = Sheets("Data")

    Dim wksIn As Worksheet
    Set wksIn = Sheets("In")

    Set rLastCell = wksIn.Cells.Find(What:="*", After:=wksIn.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)

    wksIn.Range(Cells(5, rLastCell.Column + 1), Cells(10, rLastCell.Column + 1)).Value = wksData.Range("L5:L10").Value
    wksIn.Range(Cells(11, rLastCell.Column + 1), Cells(16, rLastCell.Column + 1)).Value = wksData.Range("L13:L18").Value
End Sub

Method C

Find last column in row 5 and tack on info in next available column:

Sub offsetCol()
    Dim wksData As Worksheet
    Set wksData = Sheets("Data")

    Dim wksIn As Worksheet
    Set wksIn = Sheets("In")

    Dim rLastCol As Integer
    rLastCol = wksIn.Cells(5, wksIn.Columns.Count).End(xlToLeft).Column + 1

    wksIn.Range(Cells(5, rLastCol), Cells(10, rLastCol)).Value = wksData.Range("L5:L10").Value
    wksIn.Range(Cells(11, rLastCol), Cells(16, rLastCol)).Value = wksData.Range("L13:L18").Value
End Sub

Starting Data:

enter image description here


Results (Method C):

enter image description here

Otros consejos

Part of the question - copy from location A to location B, is not very clear, but I am guessing this is what you need. Put this under a macro of a button:

Sub Macro2()
Dim rng As Range
Sheets("IN").Range("B:B").Insert Shift:=xlToRight
Sheets("Data").Select
Set rng = Range("L5,L6,L7,L8,L9,L10,L13,L14,L15,L16,L17,L18")
rng.Copy Sheets("IN").Range("B:B")

End Sub
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top