Exporting to Notepad

mserfling

New Member
Joined
Nov 6, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hello! I've been trying to make a VBA program that automatically exports a certain portion of an excel file. This will be for multiple files that very but theres always same marker for the portion I need extracted. I am not sure why this isnt working right now. Any suggestions would be appreciated. Heres the code. :)

VBA Code:
Sub ExportToTXT()

    Dim SaveFilePath As String
    Dim DoRead As Boolean
    Dim MyRow As Integer
    Dim FoundHead As Boolean
    Dim MyLabel As Boolean
    Dim MyReadLine As String
    Dim MyCol As Integer
    Dim DeadRowCount As Integer
    Dim SaveLocRes As Variant

    SaveLocRes = Application.GetSaveAsFilename(FileFilter:="TXT Files (*.txt), *.txt", Title:="Save Output")

    If SaveLocRes <> False Then
       SaveFilePath = SaveLocRes
    Else
        Return
    End If
   
    Open SaveFilePath For Output As #1

    DoRead = True
    FoundHead = False
    MyLabel = False
    DeadRowCount = 0
    MyRow = 1
    While DoRead
        If Cells(MyRow, MyCol) <> vbNullString Then
            If Cells(MyRow, 1).Interior.ColorIndex <> 0 Then
                If Not FoundHead Then
                    MyLabel = False
                    If Cells(MyRow, 1) = "QTY. 1 EACH SIZE: .75 X 1.375" Then
                        MyLabel = True
                    End If
                End If
                FoundHead = True
            Else
                If MyLabel Then
                    MyReadLine = Cells(MyRow, 1)
                    MyCol = 2
                    While Cells(MyRow, MyCol) <> vbNullString
                        MyReadLine = MyReadLine & vbTab & Cells(MyRow, MyCol)
                        MyCol = MyCol + 1
                    Wend
                    Print #1, MyReadLine
                End If
                FoundHead = False
            End If
            DeadRowCount = 0
        Else
            DeadRowCount = DeadRowCount + 1
            If DeadRowCount >= 5 Then
                DoRead = False
            End If
        End If
        MyRow = MyRow + 1
    Wend

    Close #1
End Sub
 
Sweet that works man. THANK YOU. One more question. ? Ive been trying to make it so it would only paste the cells with data in the notepad but with the same exact format. Is there anyway of doing that?
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Explain "exact format" ?? Notepad is a Text File .... there is only so much you can do in Notepad as evidenced
by the simple menu bar selections at top of Notepad.
 
Upvote 0
Explain "exact format" ?? Notepad is a Text File .... there is only so much you can do in Notepad as evidenced
by the simple menu bar selections at top of Notepad.
Sorry. I meant just removing the last "tab" off of each row. But keeping the tabs between each word.
 
Upvote 0

Forum statistics

Threads
1,215,217
Messages
6,123,675
Members
449,116
Latest member
HypnoFant

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