+ Reply to Thread
Results 1 to 8 of 8

Assigning/Applying Variables to Copy/Paste Columns

  1. #1
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Post Assigning/Applying Variables to Copy/Paste Columns

    Hello Macro Veterans,

    I am trying to write a macro that filters a worksheet, and then copies over certain columns to another worksheet in the same workbook. I want to assign variables because new columns are frequently added, changing the coordinates of other column titles. As of now, I have the variables working for the filtering part, but I am not sure how to create/apply variables to the part of the code that grabs the column and pastes it into another worksheet. I have placed dotted lines around the part of the code that I am having trouble with. It is currently hard-coded with the U2:U, but variables are necessary for flexibility.

    The worksheet i want to copy from is "MOD"
    The worksheet i want to copy to is "NO WELCOME OR 30MDL"

    Hope someone can help, thank you.

    Nick


    Please Login or Register  to view this content.
    Last edited by Gard5096; 12-05-2012 at 03:22 PM. Reason: clarification

  2. #2
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Assigning/Applying Variables to Copy Columns

    Does nobody know how to do this?

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assigning/Applying Variables to Copy/Paste Columns

    A copy command inherently will ignore rows hidden by an AutoFilter. So you don't need to worry about setting the range. Use the whole range and only the visible rows will be copied.

    You don't even need the SpecialCells function.

    Please Login or Register  to view this content.

    Are you implying column U itself needs to be variable? What method would you employ for finding that column first?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Re: Assigning/Applying Variables to Copy/Paste Columns

    Jerry, thank you for replying.

    There are going to be 40+ columns in the worksheet, and after filtering 2-3 of them, I will need to copy entire columns. Some of the cells in these columns that are being copied are blank, which is why I used the special cells function. I need to copy all null and not null cells in the desired columns, is this function still not necessary?

    As for the column U being a variable, I believe that's what I want to do. Each column has a title is the first row, but new columns may be added periodically. I want to identify the title of the column I need, and then copy the all cells in that column from row 2 until the bottom of the data set.

    The purpose of this is to not have to change the coordinates from U2:U to V2:V when new data is added in column F or L.


    I hope this explains it a little better. Let me know if you need more clarification.

    Thank you,

    Nick

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assigning/Applying Variables to Copy/Paste Columns

    So, use the technique you've already used to find the first two columns to find the 3rd. Then use the CELLS() method instead of the RANGE() method to call the values from that column because CELLS() lets you use a number for the column if you want.

    Maybe this is all you need:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Re: Assigning/Applying Variables to Copy/Paste Columns

    this worked great, you are my hero!! thank you.

  7. #7
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Re: Assigning/Applying Variables to Copy/Paste Columns

    I actually have one more question on your coding. Why did you put the range as ("1:1") instead of ("A1:ZZ1")?

    i = Application.WorksheetFunction.Match("MGR_NAME", Range("1:1"), 0)

    Thank you

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assigning/Applying Variables to Copy/Paste Columns

    I have a habit of not writing things that are "version" specific if it can be helped. Excel 2003 doesn't have a column called ZZ. Also, there's no reason to define a range of columns when pointing the the entire row 1 (however many columns that may be) does the job.

    If that takes care of your original query, please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1