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