Macro To Create A .txt File

excelnube

Board Regular
Joined
Jul 14, 2011
Messages
65
Hi,

I'm trying to create a .txt file from the data in my spreadsheet.

Requirements:

1) Copy columns A C D J S
2) Where the value in Column S = Out Of Stock
3) Each column to be separated by **
4) Each row to be separated by *split*
5) Remove *split* from the last record

Example of output file:

A**C**D**J**S*split*
0001**3**01-12-2012**Refund sent**Out of Stock*split*
0002**4**02-12-2012**Sent for fabrication**Out of Stock*split*
0003**4**03-12-2012**All items in red**Out of Stock*split*
0004**1**28-11-2012**Deliver on Mondays only**Out of Stock

My code so far:

Code:
Sub Create_OutOfStocktxt_File()
    Dim fs As Object, a As Object, i As Integer, z As String, t As String, l As String, mn As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("C:\\OutOfStock.txt", True)
    Dim r As Long
    Dim c As Long
    Dim Cols
    
    Cols = Split("A C D J S")
    
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        z = ""
    For c = 0 To UBound(Cols)
        z = z & Cells(r, Cols(c)) & "*Split*"
    Next c
        a.writeline z
    Next r
    
End Sub

Any help appreciated.

Thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:

Code:
        If IsError(Range("S" & r).Value) Then
            Range("S" & r).ClearContents
        End If

Thanks, tried an is falling over at:

Code:
        If IsError(Range("S" & r).Value)

Hovering over Range, error produced is: Range("S" & r).Value = <Method 'Range' of Object '_Global' failed>

Current code:

Code:
Dim fs As Object
    Dim a As Object
    Dim Cols As Variant
    Dim r As Long
    Dim z As String
    Dim c As Long
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("C:\\OutOfStock.txt", True)
    Cols = Split("A C D J S")
    
    If IsError(Range("S" & r).Value) Then
            Range("S" & r).ClearContents
    End If
    
    
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("S" & r).Value = "Out of Stock" Then
            z = ""
            For c = LBound(Cols) To UBound(Cols)
                z = z & "**" & Cells(r, Cols(c)).Text
            Next c
            z = z & "*Split*"
            a.writeline z
        End If
    Next r
 
Upvote 0
Sorry, I missed that requirement:

Code:
If r < Range("A" & Rows.Count).End(xlUp).Row Then z = z & "*Split*"
 
Upvote 0
Sorry, I missed that requirement:

Rich (BB code):
If r < Range("A" & Rows.Count).End(xlUp).Row Then z = z & "*Split*"

Thanks, tried this however the final record in the .txt file is still displaying *Split*

My code:

Code:
Dim fs As Object
    Dim a As Object
    Dim Cols As Variant
    Dim r As Long
    Dim z As String
    Dim c As Long
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("C:\\OutOfStock.txt", True)
    Cols = Split("A C D J S")
    
    For r = 1 To Range("A" & Rows.Count).End(xlUp).Row
    
        If IsError(Range("S" & r).Value) Then
            Range("S" & r).ClearContents
        End If
    
        If Range("S" & r).Value = "Out of Stock" Then
            z = ""
            For c = LBound(Cols) To UBound(Cols)
                z = z & "**" & Cells(r, Cols(c)).Text
            Next c
                z = z & "*Split*"
            a.writeline z
        End If
        
        [B]If r < Range("A" & Rows.Count).End(xlUp).Row Then z = z & "*Split*"[/B]
        
    Next r
 
Upvote 0
I guess the last row didn't contain "Out of Stock". Try replacing:

Code:
If r < Range("A" & Rows.Count).End(xlUp).Row Then z = z & "*Split*"

with:

Code:
If WorksheetFunction.CountIf(Range("S" & r + 1 & ":S" & LR), "Out of Stock") > 0 Then z = z & "*Split*"
 
Upvote 0
I guess the last row didn't contain "Out of Stock". Try replacing:

Rich (BB code):
If r < Range("A" & Rows.Count).End(xlUp).Row Then z = z & "*Split*"

with:

Rich (BB code):
If WorksheetFunction.CountIf(Range("S" & r + 1 & ":S" & LR), "Out of Stock") > 0 Then z = z & "*Split*"

Should:

Rich (BB code):
If WorksheetFunction.CountIf(Range("S" & r + 1 & ":S" & LR), "Out of Stock") > 0 Then z = z & "*Split*"

Be LR, or just R ?
 
Upvote 0
Additionally, ** is being added to the start of every row e.g

**00001**05-12-2012**....

Whereas im after:

00001**05-12-2012**...
 
Upvote 0
Sorry, I forgot that I made some additional changes:

Code:
Sub Create_OutOfStocktxt_File()
    Dim fs As Object
    Dim a As Object
    Dim Cols As Variant
    Dim LR As Long
    Dim r As Long
    Dim z As String
    Dim c As Long
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile(CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator & "OutOfStock.txt", True)
    Cols = Split("A C D J S")
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For r = 1 To LR
        If IsError(Range("S" & r).Value) Then
            Range("S" & r).ClearContents
        End If
        If Range("S" & r).Value = "Out of Stock" Then
            z = ""
            For c = LBound(Cols) To UBound(Cols)
                z = z & "**" & Cells(r, Cols(c)).Text
            Next c
            If WorksheetFunction.CountIf(Range("S" & r + 1 & ":S" & LR), "Out of Stock") > 0 Then z = z & "*Split*"
            a.writeline z
        End If
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top