VBA Pisanje tekstualne datoteke | Napišite Excel podatke u tekstualne datoteke pomoću VBA koda

Excel VBA Pisanje tekstualne datoteke

U VBA možemo otvoriti ili pročitati ili napisati tekstualnu datoteku, pisanje tekstualne datoteke znači podatke koje imamo u excel listu i želimo ih u tekstualnu datoteku ili datoteku bilježnice, postoje dvije metode, jedna je po koristeći svojstvo objekta File System VBA, a drugo je pomoću metode Open and write u VBA.

U većini korporativnih tvrtki, nakon što izvještaj bude finaliziran, nastoje ga prenijeti u bazu podataka. Za prijenos u bazu podataka koriste format "Tekstualne datoteke" za ažuriranje baze podataka. Podatke obično kopiramo iz Excela i lijepimo u tekstualnu datoteku. Razlog zašto se oslanjamo na tekstualne datoteke jer je s njima vrlo jednostavno raditi zbog laganih i jednostavnijih načina. Korištenjem VBA kodiranja možemo automatizirati zadatak kopiranja podataka iz excel datoteke u tekstualnu datoteku. U ovom ćemo vam članku pokazati kako kopirati ili zapisati podatke iz excel datoteke u tekstualnu datoteku pomoću VBA koda.

Kako zapisati podatke u tekstualne datoteke pomoću VBA?

Zapisivanje podataka iz Excela u tekst složeno je kodiranje i zahtijeva vrlo dobro poznavanje VBA kodiranja. Slijedite korake u nastavku za pisanje VBA koda za kopiranje podataka iz Excela u tekstualnu datoteku.

Prije nego što vam pokažem način pisanja koda, objasnit ću vam kako otvoriti tekstualnu datoteku pomoću otvorene izjave.

Sintaksa otvorene tekstualne datoteke

Otvorite [Put datoteke], za [Način], Kao [Broj datoteke]

Put do datoteke: put do datoteke koju pokušavamo otvoriti na računalu.

Način: Način je kontrola nad otvaranjem tekstualnih datoteka. Možemo imati tri vrste nadzora nad tekstualnom datotekom.

  • Način unosa: To sugerira kontrolu samo za čitanje početne tekstualne datoteke. Ako koristimo "Način unosa", ne možemo ništa učiniti s datotekom. Možemo samo pročitati sadržaj tekstualne datoteke.
  • Izlazni način: Pomoću ove opcije možemo na nju upisati sadržaj. Ovdje moramo imati na umu da će svi postojeći podaci biti prebrisani. Dakle, moramo paziti na mogući gubitak starih podataka.
  • Način dodavanja: Ovaj je način potpuno suprotan načinu izlaza. Pomoću ove metode nove podatke možemo zapravo zapisati na kraj postojećih podataka u datoteku.

Broj datoteke: Ovo će brojati broj tekstualne datoteke svih otvorenih tekstualnih datoteka. To će prepoznati brojeve otvorenih datoteka u cjelobrojnim vrijednostima od 1 do 511. Dodjela broja datoteke je nezgodna i dovodi do velike zabune. Za to možemo koristiti besplatnu funkciju Datoteka.

Besplatna datoteka vraća jedinstveni broj otvorenih datoteka. Na taj način možemo dodijeliti jedinstveni broj datoteke bez ikakvih dupliciranih vrijednosti.

Ovdje možete preuzeti ovaj predložak za pisanje tekstualne datoteke VBA - VBA predložak za pisanje tekstualne datoteke

Primjer # 1

Slijedite korake u nastavku za pisanje koda za stvaranje nove tekstualne datoteke.

Pretpostavimo da ste u memoriji računala već imali tekstualnu datoteku pod nazivom "Hello.txt" i mi ćemo vam pokazati kako u nju upisati podatke.

Korak 1: Deklarirajte varijablu

Proglasite varijablu koja će držati put do datoteke kao String.

Kodirati:

 Sub TextFile_Example1 () Zatamni put kao kraj niza Sub 

Korak 2: Odredite broj datoteke

Da bismo utvrdili na koji se broj datoteke pozivamo, deklarirati još jednu varijablu kao Integer.

Kodirati:

 Sub TextFile_Example1 () Zatamni put kao niz Priguši broj datoteke kao cjeloviti kraj Sub 

Korak 3: Dodijelite put datoteke

Sada za varijablu Path dodijelite putanju datoteke s imenom datoteke.

Kodirati:

 Sub TextFile_Example1 () Zatamni put kao niz Priguši broj datoteke kao cjeloviti put = "D: \ Excel datoteke \ VBA datoteka \ Hello.txt" 'Promijenite put prema vašem zahtjevu Kraj Sub 

Korak 4: Dodijelite funkciju besplatne datoteke

Sada varijabli Broj datoteke dodijelite funkciju "Besplatna datoteka" za pohranu jedinstvenog broja datoteke.

Kodirati:

 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 End Sub 

Step 5: Open Text File

Now we need to open the text file to work with it. As I have explained we need to use the OPEN statement to open the text file.

Step 6: Use Print/Write Method

Once the File is opened we need to write something in it. To write in the text file we need to use either the “Write” or “Print” method.

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" 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.