+ Reply to Thread
Results 1 to 14 of 14

copy from another workbook's worksheet based on column name.

  1. #1
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    copy from another workbook's worksheet based on column name.

    I am trying to search through a workbook that only will have 1 tab. I want to search for column names, as sometimes they will have less columns depending on how the reports are generated.

    I want to copy that column, minus the header, into a table on a worksheet in a completely different workbook. I use the OpenFileDialog to select the report to copy from. The VBA code is on the workbook that is being pasted into, so it will be open.

    Here is the code I have tried. I get a "Run-time error '13': Type Mistmatch" Error
    Please Login or Register  to view this content.
    Also tried this
    Please Login or Register  to view this content.
    this code is in a module on the workbook being copied into.
    Last edited by Jhail83; 07-31-2013 at 03:33 PM.

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

    Re: copy from another workbook's worksheet based on column name.

    One error... wsImport is declared and used as a worksheet object, not a string. So you would use it just like any other sheet object.

    Please Login or Register  to view this content.

    Note, you can't copy an entire column from one sheet and paste into A2 of another sheet, only A1.
    _________________
    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!)

  3. #3
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    Quote Originally Posted by JBeaucaire View Post
    One error... wsImport is declared and used as a worksheet object, not a string. So you would use it just like any other sheet object.

    Please Login or Register  to view this content.

    Note, you can't copy an entire column from one sheet and paste into A2 of another sheet, only A1.
    Yeah I am gonna work on fixing leaving out the header.


    When I try to copy it in the workbook the code is in, into another Worksheet on the same workbook even I still get an error.
    It's not what I am really trying to accomplish, but not even this will work:

    Error: "Run-Time error '1004': Method 'Select' of object'_Worksheet' failed"

    Please Login or Register  to view this content.
    It apparently won't even find the sheet in the same workbook as the code... nevermind another workbook.
    Last edited by Jhail83; 07-31-2013 at 01:24 PM.

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

    Re: copy from another workbook's worksheet based on column name.

    Subscript out of range means an object you have named cannot be found. This usually means a sheet name is wrong or a workbook name is wrong.

    The most common mistake is hidden spaces or outright typos. I see "sheet 4" in your code.... should that be "Sheet4" ?



    I've explained and demonstrated a correction for your use of wsImport and wsExport objects. Keep reading the previous reply until you understand what I've shown, then try it.

  5. #5
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    sorry, I updated my previous post.

    I understand that I was using the wrong selection syntax, I missed that part before editing.

    now I am getting this...

    Error: "Run-Time error '1004': Method 'Select' of object'_Worksheet' failed"

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

    Re: copy from another workbook's worksheet based on column name.

    Always click the DEBUG button and present the line of code highlighted as the error.

  7. #7
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    It crashes when I hit debug.

    And when I try to run it in debug mode and step through, it crashes when it gets to

    wsImport.Select

  8. #8
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    tried getting away from using select, but it crashes when it gets to:
    intColNum = WorksheetFunction.Match("Publisher", wsExport.Range("1:1"), 0)

    Error: "Run-time error '1004': Method 'Range' of object'_Worksheet'failed"




    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    Test.zip
    Here are the workbooks I am working with.

    The code in question is in the Database Template.xlsm file. I want to open that workbook, and on the first tab, hit the button (for now, trying to get the code right. Will probably implement it a little better later). Then open the export.xlsx workbook, and have it search for the column with "Publisher" as the header and copy that column into the "Publisher" Column on the "Import Data" tab in the Database Template.xlsm file.

    If you or anyone else can help, I would appreciate it very much!

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

    Re: copy from another workbook's worksheet based on column name.

    The RANGE() method requires you to use column "letters", the CELLS() method lets you use column numbers, optionally

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    Got it!!! THANKS!


    Hopefully the rest goes smoothly now.
    Please Login or Register  to view this content.

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

    Re: copy from another workbook's worksheet based on column name.

    The macro you pasted above is still wrong, the corrections I suggested are not there.

  13. #13
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    Re: copy from another workbook's worksheet based on column name.

    Oh I stuck with range, I just converted the Column number to a letter. Would you recommend using your method anyway?

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

    Re: copy from another workbook's worksheet based on column name.

    Yes, if column numbers are spotted into a Long Variable, best to go ahead and use it that way. The less plumbing the better.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How do i select lines based on value in one Column and copy to other Workbook
    By dappelma in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-18-2013, 05:38 PM
  2. Copy a row if an X is in a column to a worksheet in a workbook
    By Bazmama in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2013, 04:20 AM
  3. Replies: 2
    Last Post: 12-26-2012, 09:36 AM
  4. Replies: 5
    Last Post: 06-08-2012, 11:26 PM
  5. Replies: 3
    Last Post: 01-15-2005, 11:06 AM

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