'Edit these to what you want
Const OutputFileName As String = "output.txt" 'The name of your output file
Const FilePathCellAddress As String = "$A$1" 'Where the file path is stored in your sheet
'Tests if a character is a number
Private Function IsNum(ByVal InputVal As String) As Boolean
IsNum = Asc(InputVal) >= 48 And Asc(InputVal) <= 57
End Function
'Tests if a character is a letter
Private Function IsLtr(ByVal InputVal As String) As Boolean
IsLtr = (Asc(InputVal) >= 65 And Asc(InputVal) <= 90) Or (Asc(InputVal) >= 97 And Asc(InputVal) <= 122)
End Function
Private Function MeetsRequirements(ByVal InputVal As String) As Boolean
'Test the String Composition (either AA### or AAAA#, where A is a letter and # is a number) and the string length.
If Len(InputVal) = 5 _
Then MeetsRequirements = ( _
IsLtr(Mid(InputVal, 1, 1)) And _
IsLtr(Mid(InputVal, 2, 1)) And _
IsNum(Mid(InputVal, 3, 1)) And _
IsNum(Mid(InputVal, 4, 1)) And _
IsNum(Mid(InputVal, 5, 1)) _
) Or ( _
IsLtr(Mid(InputVal, 1, 1)) And _
IsLtr(Mid(InputVal, 2, 1)) And _
IsLtr(Mid(InputVal, 3, 1)) And _
IsLtr(Mid(InputVal, 4, 1)) And _
IsNum(Mid(InputVal, 5, 1)) _
) _
Else: MeetsRequirements = False
End Function
'This is the macro you'll run in your sheet
Sub Save_Active_Sheet_As_Text()
Dim EndRow As Long
Dim EndCol As Long
Dim FilePath As String
FilePath = Range("$A$1").Value
With ActiveSheet.Cells
EndRow = .Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
EndCol = .Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End With
Dim FileNum As Integer
FileNum = FreeFile
Open FilePath & OutputFileName For Output As FileNum
Dim CurVal As String
Dim CurRow As Long
Dim CurCol As Long
For CurCol = 1 To EndCol
For CurRow = 1 To EndRow
CurVal = Cells(CurRow, CurCol).Value
If MeetsRequirements(CurVal) Then Print #FileNum, CurVal
Next CurRow
Next CurCol
Close FileNum
MsgBox "File " & OutputFileName & " successfully saved in " & FilePath, vbInformation, "Success"
End Sub