VBA เขียนไฟล์ข้อความ | เขียนข้อมูล Excel เป็นไฟล์ข้อความโดยใช้รหัส VBA

Excel VBA เขียนไฟล์ข้อความ

ใน VBA เราสามารถเปิดหรืออ่านหรือเขียนไฟล์ข้อความได้การเขียนไฟล์ข้อความหมายถึงข้อมูลที่เรามีอยู่ในแผ่นงาน excel และเราต้องการให้เป็นไฟล์ข้อความหรือไฟล์แผ่นจดบันทึกมีสองวิธีในการทำวิธีหนึ่งคือ โดยใช้คุณสมบัติอ็อบเจ็กต์ระบบไฟล์ของ VBA และอีกอย่างคือใช้วิธีเปิดและเขียนใน VBA

ใน บริษัท องค์กรส่วนใหญ่เมื่อสรุปรายงานแล้วพวกเขาจะอัปโหลดรายงานไปยังฐานข้อมูล ในการอัปโหลดไปยังฐานข้อมูลจะใช้รูปแบบ "ไฟล์ข้อความ" เพื่ออัปเดตฐานข้อมูล โดยปกติเราจะคัดลอกข้อมูลจาก excel และวางลงในไฟล์ข้อความ เหตุผลที่เราพึ่งพาไฟล์ข้อความเนื่องจากไฟล์เหล่านี้ใช้งานได้ง่ายมากเนื่องจากมีน้ำหนักเบาและง่ายกว่า การใช้การเข้ารหัส VBA ทำให้เราสามารถคัดลอกข้อมูลจากไฟล์ excel ไปยังไฟล์ข้อความได้โดยอัตโนมัติ ในบทความนี้เราจะแสดงวิธีคัดลอกหรือเขียนข้อมูลจากไฟล์ excel ไปยังไฟล์ข้อความโดยใช้รหัส VBA

วิธีเขียนข้อมูลเป็นไฟล์ข้อความโดยใช้ VBA

การเขียนข้อมูลจาก excel เป็นข้อความเป็นการเข้ารหัสที่ซับซ้อนและต้องการความรู้เกี่ยวกับการเข้ารหัส VBA เป็นอย่างดี ทำตามขั้นตอนด้านล่างเพื่อเขียนโค้ด VBA เพื่อคัดลอกข้อมูลจาก excel ไปยังไฟล์ข้อความ

ก่อนที่ฉันจะแสดงวิธีการเขียนโค้ดให้ฉันอธิบายวิธีเปิดไฟล์ข้อความโดยใช้คำสั่งเปิด

ไวยากรณ์ของไฟล์ข้อความเปิด

เปิด [File Path] สำหรับ [Mode] เป็น [File Number]

File Path:เส้นทางของไฟล์ที่เราพยายามเปิดบนคอมพิวเตอร์

โหมด:โหมดคือการควบคุมที่เราสามารถมีได้ในการเปิดไฟล์ข้อความ เราสามารถควบคุมไฟล์ข้อความได้สามประเภท

  • โหมดการป้อนข้อมูล:สิ่งนี้แนะนำการควบคุม " อ่านอย่างเดียว " ของไฟล์ข้อความที่เปิด หากเราใช้“ โหมดอินพุต” เราจะไม่สามารถทำอะไรกับไฟล์ได้ เราสามารถอ่านเนื้อหาของไฟล์ข้อความ
  • โหมดเอาต์พุต:การใช้ตัวเลือกนี้เราสามารถเขียนเนื้อหาได้ จุดที่เราต้องจำไว้ที่นี่คือข้อมูลที่มีอยู่ทั้งหมดจะถูกเขียนทับ ดังนั้นเราต้องระวังการสูญหายของข้อมูลเก่าที่อาจเกิดขึ้น
  • Append Mode:โหมดนี้ตรงข้ามกับโหมด OutPut โดยสิ้นเชิง การใช้วิธีนี้เราสามารถเขียนข้อมูลใหม่ที่ส่วนท้ายของข้อมูลที่มีอยู่ในไฟล์ได้

หมายเลขไฟล์:จะนับจำนวนไฟล์ข้อความของไฟล์ข้อความที่เปิดทั้งหมด สิ่งนี้จะจดจำหมายเลขไฟล์ที่เปิดเป็นค่าจำนวนเต็มตั้งแต่ 1 ถึง 511 การกำหนดหมายเลขไฟล์เป็นเรื่องยุ่งยากและนำไปสู่ความสับสนอย่างมาก สำหรับสิ่งนี้เราสามารถใช้ฟังก์ชัน File ฟรี

Free Fileส่งคืนหมายเลขเฉพาะสำหรับไฟล์ที่เปิด ด้วยวิธีนี้เราสามารถกำหนดหมายเลขไฟล์เฉพาะโดยไม่มีค่าซ้ำกัน

คุณสามารถดาวน์โหลดเทมเพลตไฟล์ข้อความการเขียน VBA ได้ที่นี่ - เทมเพลตไฟล์ข้อความเขียน VBA

ตัวอย่าง # 1

ทำตามขั้นตอนด้านล่างเพื่อเขียนโค้ดเพื่อสร้างไฟล์ข้อความใหม่

สมมติว่าคุณมีไฟล์ข้อความชื่อ“ Hello.txt” อยู่แล้วในที่เก็บข้อมูลของคอมพิวเตอร์และเราจะแสดงวิธีการเขียนข้อมูลในนั้น

ขั้นตอนที่ 1: ประกาศตัวแปร

ประกาศตัวแปรเพื่อยึดเส้นทางของไฟล์เป็น String

รหัส:

 Sub TextFile_Example1 () Dim Path เป็น String End Sub 

ขั้นตอนที่ 2: กำหนดหมายเลขไฟล์

ในการพิจารณาว่าหมายเลขไฟล์ใดที่เราอ้างถึงเพื่อประกาศอีกหนึ่งตัวแปรเป็นจำนวนเต็ม

รหัส:

 Sub TextFile_Example1 () Dim Path As String Dim FileNumber As Integer End Sub 

ขั้นตอนที่ 3: กำหนดเส้นทางไฟล์

ตอนนี้สำหรับตัวแปร Path กำหนดเส้นทางไฟล์ด้วยชื่อไฟล์

รหัส:

 Sub TextFile_Example1 () Dim Path As String Dim FileNumber As Integer Path = "D: \ Excel Files \ VBA File \ Hello.txt" 'เปลี่ยนเส้นทางตามความต้องการของคุณ End Sub 

ขั้นตอนที่ 4: กำหนดฟังก์ชันไฟล์ฟรี

ตอนนี้สำหรับตัวแปร File Number กำหนดฟังก์ชัน“ Free File” เพื่อจัดเก็บหมายเลขไฟล์เฉพาะ

รหัส:

 Sub TextFile_Example1 () Dim Path As String Dim FileNumber As Integer Path = "D: \ Excel Files \ VBA File \ Hello.txt" 'เปลี่ยนเส้นทางตามความต้องการของคุณ FileNumber = FreeFile End Sub 

ขั้นตอนที่ 5: เปิดไฟล์ข้อความ

ตอนนี้เราต้องเปิดไฟล์ข้อความเพื่อใช้งานได้ ตามที่ฉันได้อธิบายไปแล้วเราจำเป็นต้องใช้คำสั่ง OPEN เพื่อเปิดไฟล์ข้อความ

ขั้นตอนที่ 6: ใช้วิธีพิมพ์ / เขียน

เมื่อเปิดไฟล์แล้วเราจำเป็นต้องเขียนอะไรบางอย่างในนั้น ในการเขียนไฟล์ข้อความเราจำเป็นต้องใช้วิธี "เขียน" หรือ "พิมพ์"

รหัส:

 Sub TextFile_Example1 () Dim Path As String Dim FileNumber As Integer Path = "D: \ Excel Files \ VBA File \ Hello.txt" 'เปลี่ยนเส้นทางตามความต้องการของคุณ FileNumber = FreeFile Open Path For Output As FileNumber Print #FileNumber, " ยินดีต้อนรับ "พิมพ์ #FileNumber" เป็น "พิมพ์ #FileNumber" VBA "End Sub 

First, we need to mention the file number (here we have assigned the file through the “FileNumber” variable), then we need to add the content we want to add to a text file.

Step 7: Save and Close Text File

Once the content is written in a text file, we need to save and close the text file.

Code:

 Sub TextFile_Example1() Dim Path As String Dim FileNumber As Integer Path = "D:\Excel Files\VBA File\Hello.txt" 'Change the path as per your requirement FileNumber = FreeFile Open Path For Output As FileNumber Print #FileNumber, "Welcome" Print #FileNumber, "to" Print #FileNumber, "VBA" Close FileNumber End Sub 

Now, run the code this manually or through shortcut excel key F5, it will write the mentioned content in the mentioned text file.

Example #2

Now we will see how to write the data of excel sheet to a text file.

For this example, I have created simple data in excel like below.

Step 1: With the continuation of the old example define two more variables as Integer to find the last row and last column.

Code:

 Sub TextFile_Example2() Dim Path As String Dim FileNumber As Integer Dim LR As Integer Dim LC As Integer End Sub 

Step 2: Find the last used row and column in the worksheet.

Step 3: Now assign the file path and file number.

Step 4: Now use the OPEN statement to open the text file.

Step 5: We need to loop through rows and columns, so declare two more variables as Integer.

Step 6: Now open Loop to loop through the row (For next loop in VBA)

Step 7: Now to loop through columns open one more loop inside the existing loop.

Step 8: We need to write the same line of data until it reaches the last column. So for this apply IF statement in VBA.

Step 9: Now save and close the text file.

This code will write the details to a text file, but to open the text file after written we need to use the below code.

Code:

 Sub TextFile_Example2() Dim Path As String Dim FileNumber As Integer Dim LR As Integer Dim LC As Integer Dim k As Integer Dim i As Integer LR = Worksheets("Text").Cells(Rows.Count, 1).End(xlUp).Row LC = Worksheets("Text").Cells(1, Columns.Count).End(xlToLeft).Column Path = "D:\Excel Files\VBA File\Hello.txt" FileNumber = FreeFile Open Path For Output As FileNumber For k = 1 To LR For i = 1 To LC If i  LC Then Print #FileNumber, Cells(i, k), Else Print #FileNumber, Cells(i, k) End If Next i Next k Close FileNumber Shell "notepad.exe " & Path, vbNormalFocus End Sub 

So, run the code using the F5 key or manually then, it will copy the data like the below.