Count words in a line of a txt file

Ridelmou

New Member
Joined
Apr 12, 2024
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone.
Im working on a small macro that brings coordinates of points as (P X Y Z D) format.
Ex:
1 4354 5456. 43.40 R
2 5432 6578 45.78 Corner
3 6578 6765 56.76
4 6548 9806 56.99 R
.
.
... Etc
The probleme is that some files doesnt contain the D column.
I need to count words or spaces or if there is a way to count columns in text files.
Im nee bee btw.
Thank u
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
What is your expected output?

4
4
3
4

?
 
Upvote 1
VBA Code:
Sub jec()
 Dim tLine
 Open "C:\Users\xxxx\Downloads\testf.txt" For Input As #1
 
 Do Until EOF(1)
   Line Input #1, tLine
   Debug.Print UBound(Split(tLine)) + 1
 Loop

 Close #1
End Sub
 
Upvote 1
Solution
VBA Code:
Sub jec()
 Dim tLine
 Open "C:\Users\xxxx\Downloads\testf.txt" For Input As #1
 
 Do Until EOF(1)
   Line Input #1, tLine
   Debug.Print UBound(Split(tLine[B][U],vbTab[/U][/B])) + 1
 Loop

 Close #1
End Sub
I implemented the code and added vbTab in split Function as my data is sparated by Tabs.
Thank you very much
 

Attachments

  • Screenshot 2024-04-13 211034.png
    Screenshot 2024-04-13 211034.png
    64.1 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,215,259
Messages
6,123,919
Members
449,135
Latest member
NickWBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top