nhờ viết code chuyển file excel sang file mẫu .txt

Liên hệ QC
Tôi tuân thủ nội quy khi đăng bài

soap1234

Thành viên hoạt động
Tham gia
22/10/13
Bài viết
128
Được thích
5
chào anh chị giúp em chuyển file excel này sang file TXT em đính kèm ạ . em xin cảm ơn ạ
 

File đính kèm

  • Hstrich.xlsx
    11.3 KB · Đọc: 18
  • Hstrich.txt
    311 bytes · Đọc: 19
chào anh chị giúp em chuyển file excel này sang file TXT em đính kèm ạ . em xin cảm ơn ạ
Bác thử đoạn code sau nhé:

Mã:
Sub ExportToTextFile()
    Dim ws As Worksheet
    Dim rng As Range
    Dim txtFile As String
    Dim r As Long, c As Long
    Dim txt As String

    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name

    ' Set the range
    Set rng = ws.Range("A1:H6") ' Change to your range

    ' Define the text file path
    txtFile = "D:\output.txt" ' Change to your path

    ' Open the text file in write mode
    Open txtFile For Output As #1

    ' Loop through each row
    For c = 1 To rng.Columns.Count Step 2
        ' Loop through each column pair
        For r = 1 To rng.Rows.Count
            ' Write the cell values to the text file
            txt = rng.Cells(r, c).Value & " " & rng.Cells(r, c + 1).Value
            Print #1, txt
        Next r
    Next c

    ' Close the text file
    Close #1

    MsgBox "Data exported to text file successfully!", vbInformation
End Sub
 
Upvote 0
Web KT
Back
Top Bottom