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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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
Edit. Its giving me runtime error 55
 
Upvote 0
One thing observed here :

VBA Code:
MyRow = 1

    MyCol = 2  '<--- missing
 
    While DoRead
        If Cells(MyRow, [B][COLOR=rgb(226, 80, 65)]MyCol[/COLOR][/B]) <> vbNullString Then
            If Cells(MyRow, 1).Interior.ColorIndex <> 0 Then
 
Upvote 0
.
Is this the phrase your macro is looking for ? : QTY. 1 EACH SIZE: .75 X 1.375

If so, what range are you wanting to copy and write to a text file ?
 
Upvote 0
.
Is this the phrase your macro is looking for ? : QTY. 1 EACH SIZE: .75 X 1.375

If so, what range are you wanting to copy and write to a text file ?
Yea thats what its looking for. And Im trying to make it print everything below that to a txt file.
 
Upvote 0
.
Is this the phrase your macro is looking for ? : QTY. 1 EACH SIZE: .75 X 1.375

If so, what range are you wanting to copy and write to a text file ?
I've got the error to go away. Still not sure what the hell happened. But now its making a new file, except its not printing anything to the file...
 
Upvote 0
VBA Code:
Option Explicit

Sub SrchCopyRng()
Dim i As Integer
Dim Str As String
Dim LastRow As Variant

LastRow = Cells(Rows.Count, "A").End(xlUp).Row   '<-- change column here

Str = "QTY. 1 EACH SIZE: .75 X 1.375"     '<--- specify term

    For i = LastRow To 1 Step -1
       If Sheet1.Range("A" & i).Value = Str Then
          Sheet1.Range("A" & i).Resize(LastRow, 4).Copy 'Sheet2.Range("A1")
          Shell "notepad.exe", vbNormalFocus
          'Paste the range
          SendKeys "^V"
       End If
    Next i

End Sub
 
Upvote 0
Auto save selection :

VBA Code:
Option Explicit

Sub SrchCopyRng()
Dim i As Integer
Dim Str As String
Dim LastRow As Variant

LastRow = Cells(Rows.Count, "A").End(xlUp).Row   '<-- change column here

Str = "QTY. 1 EACH SIZE: .75 X 1.375"     '<--- specify term

    For i = LastRow To 1 Step -1
       If Sheet1.Range("A" & i).Value = Str Then
          Sheet1.Range("A" & i).Resize(LastRow, 4).Copy 'Sheet2.Range("A1")
          Shell "notepad.exe", vbNormalFocus
          'Paste the range
          SendKeys "^V"
          SendKeys "%fs", True

       End If
    Next i
    
Application.CutCopyMode = False

End Sub
 
Upvote 0
Wow, this works! Thank you. There is one slight issue that I didn't realize with the phrase it's looking for. It does say that phrase twice. Is there any way to ignore the first time it says it because the one I need is when it says that phrase the second time?
 
Upvote 0
This macro searches from the bottom up. So it will "grab" the last entry in your database.

If it searches from the top down, it will "grab" the first entry in your database. So ..... how many possible entries of "QTY. 1 EACH SIZE: .75 X 1.375" are there
in the database ? If there might be more than two, the macro will need to take a completely different approach.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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