Sub CopySortAndExportToTxt
Sub CopySortAndExportToTxt
' Define the source sheet and range in the current workbook
Set sourceSheet = ThisWorkbook.Sheets("Sheet1") ' Update with your sheet name
lastDataRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
Set sourceRange = sourceSheet.Range("A1:B" & lastDataRow)
' Sort the data in the new workbook (assuming sorting by the first column)
With newWorkbook.Sheets(1).Sort
.SortFields.Add Key:=Range("A1"), Order:=xlAscending
.SetRange newWorkbook.Sheets(1).UsedRange
.Header = xlYes
.Apply
End With
' Define the file path and name for the text file
filePath = "C:\Path\To\Your\File.txt" ' Update this with your desired file path
MsgBox "Data has been sorted and exported to " & filePath, vbInformation
End Sub