+ Reply to Thread
Results 1 to 10 of 10

Multiple Cells into txt file in specific order

  1. #1
    Registered User
    Join Date
    05-02-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Multiple Cells into txt file in specific order

    Hi,

    I'm new to the whole VB and macro thing, I can get my head around VlOOKUPS and IFs etc, but the programming in excel doesn't go in.

    I used to write basic vba code for file manipulation, but was a long time ago, and my skills have faded.

    Basically I need to create an xml file, but I'm happy to call it a .txt with the following output from excel.

    I have 2 sheets with data that I need I will call them Sheet 3 and Sheet 4, however they are named something else but easier for others to follow if I can get an answer.

    I need to get the data from these sheets in the following format:


    All of column U from two sheets and export them into a txt file, then add a carriage return and add all of columns AD, AE, AF, AG so they look like this:

    Output of Column U from Sheet3 and sheet4 all on new lines

    Sheet3!U:1
    Sheet3!U:2
    Sheet3!U:3….UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet4!U:1
    Sheet4!U:2
    Sheet4!U:3….UNTIL AT THE BOTTOM OF THE COLUMN
    <CarriageReturn>
    Sheet3!AD:1
    Sheet3!AD:2
    Sheet3!AD:3…
    Sheet4!AD:1
    Sheet4!AD:2
    Sheet4!AD:3….UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet3!AE:1
    Sheet3!AE:2
    Sheet3!AE:3…UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet4!AE:1
    Sheet4!AE:2
    Sheet4!AE:3….UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet3!AF:1
    Sheet3!AF:2
    Sheet3!AF:3…UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet4!AF:1
    Sheet4!AF:2
    Sheet4!AF:3….UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet3!AG:1
    Sheet3!AG:2
    Sheet3!AG:3…UNTIL AT THE BOTTOM OF THE COLUMN
    Sheet4!AG:1
    Sheet4!AG:2
    Sheet4!AG:3….UNTIL AT THE BOTTOM OF THE COLUMN

    END of file.

    I hope this is possible somehow

    L

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Multiple Cells into txt file in specific order

    randal80,

    Welcome to the forum!
    Give this a try:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    05-02-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Multiple Cells into txt file in specific order

    Hi sorry to be a noob

    but do I use Alt+F11, and if so where do I add the code? is it to workbook, or a sheet? I get an error when adding to ThisWorkBook says
    ---------------------------
    Microsoft Visual Basic for Applications
    ---------------------------
    Run-time error '9':

    Subscript out of range
    ---------------------------
    OK Help
    ---------------------------

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Multiple Cells into txt file in specific order

    randal80,

    How to use a macro:
    1. Make a copy of the workbook the macro will be run on
      • Always run new code on a workbook copy, just in case the code doesn't run smoothly
      • This is especially true of any code that deletes anything
    2. In the copied workbook, press ALT+F11 to open the Visual Basic Editor
    3. Insert | Module
    4. Copy the provided code and paste into the module
    5. Close the Visual Basic Editor
    6. In Excel, press ALT+F8 to bring up the list of available macros to run
    7. Double-click the desired macro (I named this one tgr)


    As for the Subscript out of range error, that's probably because your sheets aren't actually named "Sheet3" and Sheet4", you'll need to update the code to the correct worksheet names.

  5. #5
    Registered User
    Join Date
    05-02-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Multiple Cells into txt file in specific order

    thanks so much, nearly there!

    I think my description is a little off!

    And I cant quite read what you are getting it to do to reset it.

    So I have the following columns
    U - XML field1
    AD - Start of XML field 2
    AE - next part of 2
    AF - next part of 2
    AG - end part of 2

    So I need all column U (500 odd rows) from sheet 3 and 4, followed by
    AD row 1
    AE row 1
    AF row 1
    AG row 1

    AD row 2
    AE row 2
    AF row 2
    AG row 2

    AD row 3
    AE row 3
    AF row 3
    AG row 3
    All above AD to AG columns from sheet 3, then repeated sheet 4
    End of file

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Multiple Cells into txt file in specific order

    So it should be :

    ----
    U1
    Sheet3!AD1
    Sheet3!AE1
    Sheet3!AF1
    Sheet3!AG1
    Sheet4!AD1
    Sheet4!AE1
    Sheet4!AF1
    Sheet4!AG1


    U2
    Sheet3!AD2
    Sheet3!AE2
    Sheet3!AF2
    Sheet3!AG2
    Sheet4!AD2
    Sheet4!AE2
    Sheet4!AF2
    Sheet4!AG2

    etc
    ---

    Is that correct?

  7. #7
    Registered User
    Join Date
    05-02-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Multiple Cells into txt file in specific order

    nearly
    that was much easier way to write it!!

    ----
    Sheet3 U1
    Sheet3 U2
    Sheet3 U....
    Sheet4 U1
    Sheet4 U2
    Sheet4 U.....
    Sheet3!AD1
    Sheet3!AE1
    Sheet3!AF1
    Sheet3!AG1
    Sheet3!AD2....

    Sheet4!AD1
    Sheet4!AE1
    Sheet4!AF1
    Sheet4!AG1
    Sheet4!AD2......

    etc
    ---

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Multiple Cells into txt file in specific order

    Updated code:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-02-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Multiple Cells into txt file in specific order

    Brilliant!!

    Thanks for that, any way to remove the column headings and blank spaces if there are any? in the pages? no worries if not, I can do a search replace.

  10. #10
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Multiple Cells into txt file in specific order

    Do you mean you actually want it to start at row 2 instead of row 1?

+ 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