Consulting

Results 1 to 4 of 4

Thread: Copy the last 3 columns

  1. #1
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    2
    Location

    Copy the last 3 columns

    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.
    Last edited by msknvg; 01-04-2023 at 08:49 AM. Reason: Added code tage

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    2
    Location
    Oh yes, the objective is to copy entire columns to entire columns!
    Already changed the destination range.
    Last edited by msknvg; 01-04-2023 at 08:50 AM.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •