+ Reply to Thread
Results 1 to 12 of 12

Copy and paste Filename to left of data

  1. #1
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Question Copy and paste Filename to left of data

    Hello excel gurus,

    I have a spreadsheet that copies and pastes data from excel spreadsheets within a directory. It can copy a set number of columns depending on the known files size. I have the problem, however, that the data is not labelled in any way, shape, or form. Here is the code.
    Please Login or Register  to view this content.
    Is there any way I can copy and paste the actual filename to the left of the data that is already being copy/pasted?

    Any help you guys can give would be most appreciated.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Copy and paste Filename to left of data

    Hello,

    Like this maybe ?
    Please Login or Register  to view this content.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    This gives the name of the macro file itself and not the name of the file from which the data was copied from.

    Good idea though, I will maybe try to put it in at another point of the code.

  4. #4
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    I tried to change it
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    But it said it was an invalid command :/

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Use this instead
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    I put it in like this.
    Please Login or Register  to view this content.
    I get an "Object Required" error

    I also tried it without the .Name and it gave an "Subscript out of Range" error.

    I am very bad with vba I apologize if I used it wrong.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Oh, seems like strFile itself is already the file name. So just use this:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    Thank you that worked. The only caveat is that it only pastes it once. Sometimes the data has 4 lines of data and it won't paste it in front of the next three lines, is there any way to accomplish that?

  9. #9
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Try:
    Please Login or Register  to view this content.
    Another thing is, the way you are referencing the worksheet is very inconsistent, you have:
    Worksheets(1)
    Sheet1
    Worksheets("Sheet 1")

    Not that it is a problem now, but may cause error if your worksheet is no longer named "Sheet 1".

  10. #10
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    Thank you millz, I will make sure to make it more consistent. That fix worked perfectly.

  11. #11
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    This change solved the problem but I found that it only worked when I had the worksheet open. If I run it while on a different worksheet it gives me an application-defined or object-defined error.

  12. #12
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Try this amended code:
    Please Login or Register  to view this content.

+ 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. Copy slection and paste it in ceratian workbook with PART filename as cell value
    By Dibbley247 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-15-2012, 09:56 AM
  2. Replies: 0
    Last Post: 11-01-2012, 09:28 AM
  3. copy and paste a range of cells to the left
    By michaelram in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-04-2010, 12:24 AM
  4. Copy and paste filename
    By edk08 in forum Excel General
    Replies: 1
    Last Post: 07-03-2007, 09:05 AM
  5. VB copy paste column data to another left to right sheet across sh
    By baz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2006, 04:45 AM

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