PDA

View Full Version : Copy the last 3 columns



msknvg
01-04-2023, 06:03 AM
Hello guys,

Probably this is a dumb question but i am starting a new job and its my first time with VBA.

Basically, there is an excel file that is constantly updated. I need to copy the last 3 columns of that file to another excel. As simple as that, but my code isn't working... I'm testing with just one column, and it still doesn't work.
I leave the code below.
I can't understand how to define the some stuff that i mention in


Sub Copy()
Dim lCol As Range

lCol = Cells(1, Columns.Count).End(xlToLeft).EntireColumn
(how to define the second column?)
(how to define the third column?)
Range("how to define the range of 3 columns?").Select
Selection.Copy
Application.WindowState = xlNormal
Windows("Teste 2.xlsx").Activate
Range("D1").Select
ActiveSheet.Paste
Windows("Teste 1.xlsx").Activate
End Sub




I don't know how to define the things I mention in italics.
I don't know if it was too confusing but I would be grateful with any help.

Bob Phillips
01-04-2023, 06:42 AM
Why are you trying to copy entire columns to a limited number of rows? I would have thought you wanted to copy entire columns to entire columns, or you would copy a number of cells in columns to another place.

msknvg
01-04-2023, 07:35 AM
Oh yes, the objective is to copy entire columns to entire columns!
Already changed the destination range.

p45cal
01-04-2023, 10:27 AM
Are the columns next to each other?
Cells(1, Columns.Count).End(xlToLeft).offset(,-2).resize(,3).entirecolumn.Select
or to put it into the clipboard in a single line:
Cells(1, Columns.Count).End(xlToLeft).offset(,-2).resize(,3).entirecolumn.Copy