+ Reply to Thread
Results 1 to 31 of 31

Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

  1. #1
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hello,

    I've been lurking for a while and have found this site extremely useful. I'm trying to change a macro I used on another project. It imports the 300+ excel files I have stored in a folder into a single spreadsheet. The only problem I'm having is getting it to compile the data horizontally (not transposed, but copied and pasted "as-is" in new columns instead of rows).
    Can someone please help me with this? Here is the code as it currently stands (vertical):

    Please Login or Register  to view this content.
    Last edited by WinNuke95; 01-20-2014 at 07:40 PM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Ignore - misread your post.

  3. #3
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hi Cytop,

    Each of the excel files contains about 5 - 10 columns of data. I thought of transposing the final output but it shifts my table headings around which isn't desirable. I need to preserve the original format of each table and simply dump it into adjacent cells with blank columns between to separate them. Imagine 300 10x5 tables spanning left to right. The current code produces the tables... just top to bottom.

    Edit: ah! lol beat me to it.
    Last edited by WinNuke95; 01-20-2014 at 06:32 PM.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Untested.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Quote Originally Posted by AB33 View Post
    Untested.

    Please Login or Register  to view this content.

    No dice, but thanks for trying. Cannot use transpose. The data that's being imported has table headings that need to be preserved.

    Here's how it should post (without transpose):
    1-20-2014 5-46-52 PM.jpg
    \1

    Here's how transpose affects the import process:
    1-20-2014 5-48-38 PM.jpg
    \1

    The tables I will be using for this project are significantly more complicated and ideally I'd like to just send the macro on a mission to open, search, grab, and drop the data side by side without so much as altering the original layout or direction the data sits in the cells. I hope that makes sense.
    Last edited by WinNuke95; 01-20-2014 at 07:15 PM.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    If you can not use transpose what else would change vertical in to horizontal?

  7. #7
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Perhaps I'm not communicating it properly. Can you take a peek at the two above images from my previous message?

    Maybe I have to pinpoint the table range which copies and pastes into the new spreadsheet (rather than entire rows)?
    Last edited by WinNuke95; 01-20-2014 at 07:14 PM.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    I do not know!

    I have only looked at your code. Your code as it is now copies in to next empty row. If you want to reverse the copy, you are transposing . A transpose means, convert vertical in to horizontal and vice versal.

  9. #9
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Instead of copying the next file into a new row, it needs to be copied into a new column.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    because of this line

    Please Login or Register  to view this content.
    you can only copy to column A. If you want to copy to another column other than column A, you need to remove the entire row and change to say A1:Z

  11. #11
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Ah yes, so if my spreadsheet collection is typically 6 columns wide, I can set it to A1:F33 for example, but how would I then have this macro place the data from the next spreadsheet into column H1? G1 would be a blank column to provide a separation between files.

    Basically I need the columns to continue building out in --> direction

  12. #12
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hi -

    How about this one?
    Please Login or Register  to view this content.
    event

  13. #13
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hi Event,

    Thank you for looking at the code.

    I think your changes are on the right track, but it's copying the data on top of itself instead of expanding out to the right. Would it help if I provided exact dimensions? Or maybe we can simplify this to the point that it simply copies columns A:I, pastes them into the current worksheet, inserts a blank column, and begins the loop in the next adjacent column to the blank one?

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Add 1 to go to next column

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hmm, I added the +1 but the following string needs debugging:

    Application or object-defined error:

    sh2.UsedRange.Copy sh.Cells(1, lastColumn)


    Please Login or Register  to view this content.
    Last edited by WinNuke95; 01-21-2014 at 03:09 PM.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Change this line

    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Also change
    usedrange in to A1:F13

  18. #18
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Quote Originally Posted by AB33 View Post
    Change this line

    Please Login or Register  to view this content.

    "Object variable or With block variable not set"

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Set sh = Sheets(1)
    change it to actual sheet name , not index number. What ever the master name is, it must have a name

    I suspect that the master sheet(Sheets(1) is blank.

    If it is, use this line

    Please Login or Register  to view this content.

  20. #20
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Sorry!
    My mistake. I forget to link last column to sheet master.

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Quote Originally Posted by AB33 View Post
    Sorry!
    My mistake. I forget to link last column to sheet master.
    I think I still have a problem identifying where the work is being done. The spreadsheets I'm importing have merged cells and the workbooks are password protected to prevent users from making changes.

    I made a mistake--the files to be imported DO have a single tab with a label "Income Statement". Sorry!

    I wasn't able to incorporate the A1:F13 into the range.

    The error I currently receive is a notice regarding the
    "Object variable or With block variable not set"

    This is the code I have thus far:
    Please Login or Register  to view this content.
    Last edited by WinNuke95; 01-21-2014 at 04:45 PM.

  22. #22
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    In which line of the code the error shows?
    It would be easier to solve it if you could attach a sample, so that we can pin point the error.

  23. #23
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Sure. I'm really sorry for the trouble. I had hoped it was a simple change to the original code (contained in my first post)

    Step 1:
    Download this file into C:\Temp
    [Link to file] http://www.fileswap.com/dl/18ncvsFJv...;L_1.xlsx.html

    Step 2:
    Copy this file two more times so you have a total of three of them in the temp folder.

    Step 3:
    Run a macro from a separate spreadsheet with the following code:

    Please Login or Register  to view this content.
    Goal:
    The macro should open all files in C:\Temp and copy and paste the data into one spreadsheet where the data is displayed side by side. Essentially I'm trying to prevent someone from having to open 300+ files to manually copy and paste them when users return their completed Income Statements. Having the Income Statements placed side by side is the ultimate goal here with a blank column to separate each data set.
    Last edited by WinNuke95; 01-21-2014 at 04:59 PM.

  24. #24
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    WinNuke95,
    I can not download the file from the site. Where I am interest is when the code opens the files and then copy.
    Did you get this error when you copy the entire row? If not, I want to see the code when it errors.

    Could you run the code by stepping over, using F8 key and see which line errors.

    I want to see the object variable error.

  25. #25
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Quote Originally Posted by AB33 View Post
    WinNuke95,
    I can not download the file from the site. Where I am interest is when the code opens the files and then copy.
    Did you get this error when you copy the entire row? If not, I want to see the code when it errors.

    Could you run the code by stepping over, using F8 key and see which line errors.

    I want to see the object variable error.
    The object variable error is on this line: lastColumn = sh.Cells.Find("*", , , , xlByColumns, xlPrevious).Column + 1

    If I remove this line, I then get:


    Run-time error '1004':
    We can't do that to a merged cell.

    -> sh2.UsedRange.Copy sh.Cells(1, lastColumn)


    Please Login or Register  to view this content.

  26. #26
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Okay, do you get the same error if you use this line?

    lastColumn = sh.Cells(1, Columns.Count).End(xlToLeft).Column + 1.

    You need to unmerge cells, no option.
    Try this line first.
    Please Login or Register  to view this content.
    I split the above lines in to 2 rows to separate the error.
    Or
    try this line

    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Quote Originally Posted by AB33 View Post
    Okay, do you get the same error if you use this line?

    lastColumn = sh.Cells(1, Columns.Count).End(xlToLeft).Column + 1.

    You need to unmerge cells, no option.
    Try this line first.
    Please Login or Register  to view this content.
    I split the above lines in to 2 rows to separate the error.
    Or
    try this line

    Please Login or Register  to view this content.
    I think part of the problem is I'm not sure what your intention is because we're talking about partial codes here. I may not have enough experience to interpret what you're asking me to do.

    If I use: lastColumn = sh.Cells(1, Columns.Count).End(xlToLeft).Column + 1.
    And Remove: lastColumn = sh.Cells.Find("*", , , , xlByColumns, xlPrevious).Column + 1

    Then my error points to: sh2.UsedRange.Copy sh.Cells(1, lastColumn).
    Resulting error:
    Run-time error '1004':
    We can't do that to a merged cell.


    Regarding the coping and pasting of merged cells, if you see my original post, the code works perfectly fine for a 5x10 table containing merged cells above the table. The macro copies and pastes everything exactly as it was found in the C:\Temp files and inserts blank rows to visually separate each file's data.


    When I add the sh2.Cells.UnMerge code, I get the following error:
    "Application-defined or object-defined error"

    Please Login or Register  to view this content.
    I'm pasting the entire code each time so you can see where I've made changes to determine if I've failed to follow your instructions.

  28. #28
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Well,
    My intention is very simple. Since I could not see your sample, what ever I have written is a guess work.
    If your master sheet is blank this code will not work.
    lastColumn = sh.Cells.Find("*", , , , xlByColumns, xlPrevious).Column + 1

    I assumed you have data in master sheet.
    I then had given you another code which can work with empty master sheet.
    Please Login or Register  to view this content.
    You also said, usedrange is not working, so I gave you another option.

    Please Login or Register  to view this content.
    Which copies column A1-H13 until the last row to see if it works.

    I have also asked you to attach a sample book, but you have sent me a link. If you had attached the sample, It would have been resolved long time ago, with out going to all these circles.

  29. #29
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Apologies, I misunderstood. I thought by providing a link to the file would satisfy the sample request. I've attached the sample file.

    It's a basic income statement. The account names are in column C. The column heading is merged, and two of the titles in column B may pose an issue if the macro is ending based upon empty cells. It's not entirely necessary to copy these two cells, but I thought there would be a way to simply define which cells to copy (even if blank) and paste them into the master sheet.

    My master sheet is blank. I run the macro from my desktop to compile all of the files in the c:\temp folder.

    The original range of A1:H13 pertained to a different set of data. I attempted to modify it based upon the attached but it did not succeed.

    Thanks for your patience and effort - it's appreciated.
    Attached Files Attached Files

  30. #30
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Hi -

    Here is my updated codes;
    Please Login or Register  to view this content.
    event

  31. #31
    Registered User
    Join Date
    01-20-2014
    Location
    Fort Lauderdale, FL
    MS-Off Ver
    Microsoft Office 2013
    Posts
    16

    Re: Importing Excel Files & Copying Data Horizontally (instead of vertically) Help!

    Thank you!!

+ 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. Replies: 5
    Last Post: 01-27-2013, 09:36 PM
  2. Replies: 9
    Last Post: 10-11-2012, 07:50 AM
  3. Excel 2007 : Merging data horizontally vs. vertically
    By enhydra in forum Excel General
    Replies: 4
    Last Post: 12-12-2010, 06:16 PM
  4. [SOLVED] when importing .txt files can I separate the data horizontally
    By Chrisinct in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 07-07-2006, 02:15 PM
  5. A tough one! line up vertically/horizontally through data
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2005, 04:32 PM

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