+ Reply to Thread
Results 1 to 8 of 8

Copy Excel data to Notepad file

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Copy Excel data to Notepad file

    Hello:

    Please refer to attached file.
    I need VB code to copy data from column D into txt file.
    I have txt file for each store (ColumnA) and the name of the txt file for each store is StoreName.txt
    For Example :
    Store Name : Fayetteville has a txt file as Fayetteville.txt
    Store Name : Riverdale has a txt file as Riverdale.txt
    Store Name : SF0003 has a txt file as SF0003.txt
    and so on

    All these files are in location C:\Temp\Stores\

    So basically
    Fayetteville.txt should have 1 in the file
    Riverdale.txt should have 0 in the file and so on for other store.

    Note : Data will be in from A2 down to A12
    Let me know if you have any questions.
    Thanks.

    Riz
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: Copy Excel data to Notepad file

    Basic code:

    Option Explicit
    
    Sub sExcelToTexr()
    
    Dim lLR As Long, i As Long
    Const csFolder As String = "C:\Temp\Stores\"
    Dim sFilePath As String, sCellData As String
    
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To lLR
        sFilePath = csFolder
        sFilePath = sFilePath & Range("A" & i).Value
        sFilePath = sFilePath & ".txt"
        sCellData = Range("D" & i).Value
        
        Open sFilePath For Output As #1
        Print #1, sCellData
        Close #1
    Next 'i
    
    End Sub

    You might want to put some error checking in but it works as it is with your test data (having set up the folder/sub-folder in advance)


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    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 Excel data to Notepad file

    This should do it:

    Option Explicit
    
    Sub StoresToText()
    Dim Store As Range, fNAME As String, fPATH As String
    
    fPATH = "C:\TEMP\Stores\"               'dont' forget the final \ in this path string!
    
    For Each Store In ThisWorkbook.Sheets("Sheet1").Range("A:A").SpecialCells(xlConstants)
        If Cells(Store.Row, "D") <> "" Then
            Open fPATH & Store.Text & ".txt" For Output As #1
            Print #1, Trim(Cells(Store.Row, "D").Value)
            Close #1
        End If
    Next Store
    
    End Sub
    _________________
    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!)

  4. #4
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: Copy Excel data to Notepad file

    Hi JB:

    Works great...need liitle help further.
    My actual data starts as follows:

    Store Name : AE4:AE8 and other (0 or 1 data) starts at AH4:AH8

    Would appreciate if you can help modify the code to accomodate for this data.
    Thanks a lot

    Riz

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: Copy Excel data to Notepad file

    Thanks for the rep.

    Option Explicit
    
    Sub sExcelToTexr()
    
    Dim lLR As Long, i As Long
    Const csFolder As String = "C:\Temp\Stores\"
    Dim sFilePath As String, sCellData As String
    
    lLR = Range("AE" & Rows.Count).End(xlUp).Row
    
    For i = 8 To lLR
        sFilePath = csFolder
        sFilePath = sFilePath & Range("AE" & i).Value
        sFilePath = sFilePath & ".txt"
        sCellData = Range("AE" & i).Offset(, 3).Value
        
        Open sFilePath For Output As #1
        Print #1, sCellData
        Close #1
    Next 'i
    
    End Sub

    Regards, TMS

  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 Excel data to Notepad file

    Like so, colored in red to point it out:

    Option Explicit
    
    Sub StoresToText()
    Dim Store As Range, fNAME As String, fPATH As String
    
    fPATH = "C:\TEMP\Stores\"               'dont' forget the final \ in this path string!
    
    For Each Store In ThisWorkbook.Sheets("Sheet1").Range("AE4:AE" & Rows.Count).SpecialCells(xlConstants)
        If Cells(Store.Row, "AH") <> "" Then
            Open fPATH & Store.Text & ".txt" For Output As #1
            Print #1, Trim(Cells(Store.Row, "AH").Value)
            Close #1
        End If
    Next Store
    
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: Copy Excel data to Notepad file

    Hi Guys:

    Superb help from both.
    Thanks a lot..

    Riz

  8. #8
    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 Excel data to Notepad file

    As it appears you've reached a conclusion, I've marked this thread SOLVED for you.
    FYI, this is done through the Thread Tools located above the first post in this thread. Thanks.

+ 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. Opening Notepad a saved notepad file and copying the data on it
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2014, 06:18 PM
  2. Copy data from notepad into excel
    By platesigns in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-28-2014, 12:54 PM
  3. Copying more than one range of data from excel into more than one notepad file
    By brianQ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2013, 01:25 PM
  4. [SOLVED] Copy excel data to notepad.
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2012, 08:56 AM
  5. Extract Data from Notepad file into excel
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-21-2012, 01:31 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