+ Reply to Thread
Results 1 to 4 of 4

Customized macro to export cells to txt files?

  1. #1
    Registered User
    Join Date
    01-30-2006
    Posts
    24

    Customized macro to export cells to txt files?

    Hi.

    I'm terrible with creating macros, so I figured I'll ask you wizards for help. The actual file what I'm using is about 4mb, so I'll try to explain without uploading it, hope this is ok!

    I have a NHL player data .xlsx, from where I'd need to export players info to separate text-files. I have one cell in each row containing all the needed info from the specific row (e.g. =A1&CHAR(10)&B1&CHAR(10)&...ect). So I would also need the macro to understand those line changes for the text file.

    The cells I'd like to export are in BA4:BA934 and I'd like each text file named with the player name in cells J4:J934. Basically this would save me doing 1000 files separately, so any help is really appreciated!

  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,364

    Re: Customized macro to export cells to txt files?

    See Chip Pearson's approach to exporting and importing text files:

    http://www.cpearson.com/excel/ImpText.aspx


    Regards
    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
    Registered User
    Join Date
    01-30-2006
    Posts
    24

    Re: Customized macro to export cells to txt files?

    Thanks for the reply. However, I'm not familiar with programming language at all, so even though I understand there might be a solution, I can't figure out how to use that example...

    I made an example sheet, if anyone wants to take a look. BB-cells are the ones I'd like to export to separate txt files and there's a "complicated" formula to gather all the player data from other cells.
    Attached Files Attached Files
    Last edited by Handyy; 12-19-2010 at 07:26 AM.

  4. #4
    Registered User
    Join Date
    01-30-2006
    Posts
    24

    Re: Customized macro to export cells to txt files?

    With searching, I've managed to make a macro which outputs one cell to a txt file. However, I really can't understand how do I need to modify this to make it go through all rows until there is nothing to export. I guess this is very basic stuff, but still it goes way over my head...

    This is the macro I'd need to repeat, instead of only going through BC5 and BD5:

    Function SaveTextFile(strFile As String, strData As String, Optional bOverWrite As Boolean = False) As Boolean
    Dim iHandle As Integer, l As Long
    If Not bOverWrite Then
    If Len(Dir("strFile")) > 0 Then
    SaveTextFile = False
    Exit Function
    End If
    End If
    iHandle = FreeFile
    l = Len(strData)
    Open strFile For Binary Access Write As #iHandle Len = l
    Put #iHandle, , strData
    Close #iHandle
    SaveTextFile = True
    End Function
    Function MakeString(r As Range, Optional strDelimiter As String = vbNullString) As String
    Dim vArray As Variant, i As Long, j As Long, strTemp As String
    If r.Count = 1 Then MakeString = r.Value: Exit Function
    vArray = r.Value
    For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
    strTemp = strTemp & vArray(i, j) & strDelimiter
    Next j
    strTemp = strTemp & vbCrLf
    Next i
    strTemp = Left(strTemp, Len(strTemp) - 2)
    MakeString = strTemp
    End Function

    Sub Test()
    Dim strMyString As String, strMyFile As String, bResult As Boolean
    strMyString = MakeString(Range("BC5"))
    strMyFile = Range("BG5").Value
    bResult = SaveTextFile(strMyFile, strMyString, True)
    If bResult Then _
    MsgBox "Text file creation successful"
    End Sub
    Last edited by Handyy; 12-23-2010 at 10:58 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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