+ Reply to Thread
Results 1 to 26 of 26

[VBA] Copying data from Excel to .txt File

  1. #1
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    [VBA] Copying data from Excel to .txt File

    Hello

    I am trying to make VBA to copy data from Excel to .txt file.
    The problem is that I am not quite sure of 2 things

    1) How to define the range so that VBA should know to take a specific range for each Car make (for the first Car make the range was initiated manually). So I think the condition would be something like: If(Car make==same as Car make from rows below), then do a minimum on Mileage for that range, and this should be done for each Car make.
    .
    2) How to make the code more efficient rather than declaring a lot of variables. As there would be hundreds of entries (multiple Car makes, and after each make the lowest Mileage should be displayed). As what has been done so far for rows 2-3 should also be done for rows 4-7, and so on... But for the sake of the example I kept it short.


    Below you'll find attached the Workbook (the code is located in Module1), as well as the expected Answers in the .txt file.

    I would be more than grateful if someone could help me out with it.

    Many thanks, I appreciate it!

    Gordon
    Attached Files Attached Files
    Last edited by Gordon85; 03-23-2020 at 08:06 PM.

  2. #2
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    I've also attached the
    Please Login or Register  to view this content.

    The answers should be:

    AUTO
    CAR MAKE&MODEL:Audi A4
    01.01.2000 Date + Rent & Return Days : MondayFriday
    CAR MAKE&MODEL:Audi A4
    11.04.2000 Date + Rent & Return Days : TuesdayFriday
    Lowest Mileage is: 1200
    CAR MAKE&MODEL:BMW 320D
    05.05.2010 Date + Rent & Return Days : MondaySunday
    CAR MAKE&MODEL:BMW 320D
    02.01.2010 Date + Rent & Return Days : ThursdaySaturday
    CAR MAKE&MODEL:BMW 320D
    01.01.2009 Date + Rent & Return Days : FridaySunday
    CAR MAKE&MODEL:BMW 320D
    05.08.2012 Date + Rent & Return Days : MondayWednesday
    Lowest Mileage is: 1532

    Here is how the Excel looks like:
    Capture.JPG
    Please note that there can be hundreds of entries, this example has few entries.
    Last edited by Gordon85; 03-24-2020 at 02:25 AM.

  3. #3
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: [VBA] Copying data from Excel to .txt File

    Hi Gordon85,
    I came up with this solution, but only 1 condition has to be met: column A, the cars, has to be sorted (alfabetically):
    Please Login or Register  to view this content.
    Cheers
    Erwin
    Last edited by Eastw00d; 03-24-2020 at 02:33 AM.
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

  4. #4
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Hello Erwin,

    That works like a charm (Rep+). The only problem would be that the Code is targeting specific columns, can this be tweaked to look for specific keywords as I tried above in post #2?

    I am asking because there will be more than 1 Excel document, and the names of the column headers might vary, that's why I used the search function for specific keywords combined with Active Sheet, because I am trying to run the VBA Code from an External .xlsm file to target different .xlsx files.

    Thank you!
    Gordon
    Last edited by Gordon85; 03-24-2020 at 03:37 AM.

  5. #5
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: [VBA] Copying data from Excel to .txt File

    Sure, look, everything in Cells can be changed, as it is defined as Cells(RowNum, Colnum), and I have fixed the latter.
    That can be changed of course
    I think you know enough VBA to do it yourself, otherwise we can help you

    Cheers
    Erwin

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: [VBA] Copying data from Excel to .txt File

    try
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Thank you as well, interesting approach! Rep+

    By the way, would there also be a way to get the Date where the minimum Mileage appears and put it on the same line with the Lowest Mileage?

    Sort of like:

    Date associated with lowest Mileage, "Lowest mileage is", Lowest Mileage value

    I tried putting " " between the Car make, but silly me got it wrong I guess

    Please Login or Register  to view this content.
    Should have outputted :Car Make & Model:"Audi A4" for example, any idea why it doesen't work?

    Thank you!
    Gordon
    Last edited by Gordon85; 03-24-2020 at 08:19 AM.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: [VBA] Copying data from Excel to .txt File

    1) Please don't quote whole posts -- it's just clutter.*If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    2) Not sure, but something like this maybe...
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    According to the initial attachment and posts #1, 4 & 7 an easy VBA starter demonstration (edit v3) :

    PHP Code: 
    Sub Demo1()
          Const 
    " Lowest Mileage is :"
            
    Dim VCHWF%, R&
                
    = [{"Car*","Date*","Rent*","Return*","Mile*"}]
        
    With Sheet1.UsedRange.Rows
                C 
    Application.Match(V, .Item(1), 0):  If Application.Count(C) < UBound(VThen Beep: Exit Sub
               
    .Sort .Cells(C(1)), xlAscendingHeader:=xlYes
                H 
    = Array(.Cells(C(1)) & " : """"""" vbNewLine)
                
    Application.Index(.Item(2), , C)
                
    FreeFile
                Open ThisWorkbook
    .Path Application.PathSeparator "Answers .txt" For Output As #F
                
    Print #F, "AUTO"
            
    For 2 To .Count
                V 
    Application.Index(.Item(R), , C)
                If 
    V(1) <> W(1Then Print #F, W(2); S; W(5): W = V Else If V(5) < W(5) Then W = V
                
    Print #F, H(0); V(1); H(1); V(2); " Date + Rent & Return Days : "; V(3); " "; V(4)
            
    Next
        End With
                
    Print #F, W(2); S; W(5)
                
    Close #F
    End Sub 
    Last edited by Marc L; 03-24-2020 at 11:32 AM. Reason: car model double quotes added …

  10. #10
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Thank you!

    [QUOTE=jindon;5300224]1) Please don't quote whole posts -- it's just clutter.*If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    2) Not sure, but something like this maybe...

  11. #11
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: Hi ! Try this !

    Thank you, this works as well!
    Rep+
    Quote Originally Posted by Marc L View Post
    [INDENT]
    According to the initial attachment and posts #1, 4 & 7 an easy VBA starter demonstration :

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: [VBA] Copying data from Excel to .txt File


    Post #9 just edited : car model double quotes added …

    Thanks for the rep' !

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: [VBA] Copying data from Excel to .txt File


    New edit in post #9 :

    • Resolves the sort issue if the car model is not the first column …

    • May increase the speed for hundreds / thousands source rows.
      As the more rows, the slower Index worksheet function.
      So it's faster to use Index with a row at a time - like to not play with huge strings - and the code is a bit simplified …
    Last edited by Marc L; 03-24-2020 at 11:54 AM. Reason: typo …

  14. #14
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Thank you Marc L!

    Appreciated!

  15. #15
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Guys, would be there any other way when copying the data from the Excel to the .txt file, to have just 1 appearance of the Car name and introduce another keyword such as color where the date would also be printed?

    For example if there is something like this in the Excel file
    Capture.JPG



    To output something like this?

    Car make & model "BMW 320D"
    05.05.2010 Rent+Return Monday Sunday
    02.01.2010 Rent+Return Thursday Saturday
    01.01.2009 Rent+Return Friday Sunday
    01.01.2009 Lowest mileage is 1532
    05.05.2010 Color Red
    02.01.2010 Color Green
    01.01.2009 Color Blue
    Car make & model "Audi A4"
    02.01.2020 Rent+Return Wednesday Saturday
    04.02.2011 Rent+Return Monday Thursday
    02.01.2020 Lowest mileage is 2312
    02.01.2020 Color Yellow
    04.02.2011 Color Pink
    Car make & model "Audi A5"
    02.03.2008 Rent+Return Friday Saturday
    02.03.2008 Lowest mileage is 123124
    02.03.2008 Color Orange


    Many thanks!
    Gordon
    Attached Files Attached Files
    Last edited by Gordon85; 03-25-2020 at 07:54 AM.

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: [VBA] Copying data from Excel to .txt File


    With such weird text export format maybe I will wait until the rules will be definitively fixed according to a new attachment
    as it's the third time you modify them …
    Last edited by Marc L; 03-25-2020 at 07:39 AM.

  17. #17
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Hi,

    Sorry about that.
    Just modified. I hope it is clear.
    I didn't realize that the formatting would be compromised.


    Thank you!
    Gordon

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: [VBA] Copying data from Excel to .txt File


    You forgot the source data workbook but is it the last change, that's the question ?
    Or it's better to wait until all your final export format will be well known …

  19. #19
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Added that as well, thank you for reminding. Sorry about that, I thought the copy-paste would do.
    That's the woozie of being old.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: [VBA] Copying data from Excel to .txt File

    Please Login or Register  to view this content.

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: [VBA] Copying data from Excel to .txt File


    Second question to answer - so I expect for two answers - :

    do you need the initial order must be respected as it is - meaning the car model column is yet & well sorted -
    or for 'safety' it's better to sort the column like my post #9 demonstration does ?

  22. #22
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Thank you Jindon!

    Marc L,

    1) Initial order won't be respected as it is.
    2) I would say yes, it would be better as in post #9 for "safety" reasons, as the order would be randomly.

  23. #23
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: [VBA] Copying data from Excel to .txt File


    As my first question is in post #18 …

  24. #24
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Quote Originally Posted by Marc L View Post

    You forgot the source data workbook but is it the last change, that's the question ?
    Or it's better to wait until all your final export format will be well known …
    Hello, my apologies, I didn't see this (old and senile).
    Yes, it is the final form, I added everything. The request would be that the Car make not to repeat itself for the same car make.
    Many thanks.
    Gordon

  25. #25
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: [VBA] Copying data from Excel to .txt File


    So according to the last attachment the final version :

    PHP Code: 
    Sub Demo2()
          Const 
    """"" Lowest Mileage is : "
            
    Dim VCH$, WF%, R&, T$
                
    = [{"Car*","Date*","Rent*","Return*","Mile*","Color*"}]
        
    With Sheet1.UsedRange.Rows
                C 
    Application.Match(V, .Item(1), 0):  If Application.Count(C) < UBound(VThen Beep: Exit Sub
               
    .Sort .Cells(C(1)), xlAscendingHeader:=xlYes
                H 
    = .Cells(C(1)) & " """
                
    Application.Index(.Item(2), , C)
                
    FreeFile
                Open ThisWorkbook
    .Path Application.PathSeparator "Answers .txt" For Output As #F
                
    Print #F, "AUTO"; vbNewLine; H; W(1); D
            
    For 2 To .Count
                V 
    Application.Index(.Item(R), , C)
                If 
    V(1) <> W(1Then Print #F, W(2); S & W(5); T; vbNewLine; H; V(1); D: T = "": W = V _
                                
    Else If V(5) < W(5Then W V
                
    Print #F, V(2); " Rent+Return "; V(3); " "; V(4)
                
    vbNewLine V(2) & " Color " V(6)
            
    Next
        End With
                
    Print #F, W(2); S & W(5); T;
                
    Close #F
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  26. #26
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Re: [VBA] Copying data from Excel to .txt File

    Thank you, Rep+

+ 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. Copying data from one excel file to another based on key value
    By mfgoose in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-14-2015, 03:41 AM
  2. Copying the data from 3 files to 1 Excel file
    By spe88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2015, 03:57 AM
  3. Copying data from a file to an Excel spreadsheet
    By alexander.small in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2013, 07:24 AM
  4. copying data from txt into Excel file
    By MalgosiaMen in forum Excel General
    Replies: 1
    Last Post: 08-07-2013, 04:32 AM
  5. [SOLVED] Closed excel file. Data copying.
    By pixel34 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-04-2013, 03:27 PM
  6. [SOLVED] I need a macro for copying the data of all excel files into one sheet of a excel file.
    By pavan_yuvaraj in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-18-2012, 09:23 AM
  7. copying data from several files to one excel file
    By sharman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-02-2006, 09:40 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