# i have problem counter a words in txt file in VBA

Page 1 of 1

## 0 Replies - 1444 Views - Last Post: 11 June 2008 - 03:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=54457&amp;s=4f5ba18e59e54743426849f347dc7663&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lodhe1

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 11-June 08

# i have problem counter a words in txt file in VBA

Posted 11 June 2008 - 03:17 AM

Hi all i need a program to do the following in command button i did but it is not working properly.
my code as follow
Public FileName As String

Private Sub CommandButton1_Click()
Dim c As String
Dim FileName As String
Dim word As String
'Dim c As String
Dim i As Integer
Dim FileNo As String
Dim w() As Characters
i = 1
c = FreeFile
Open_File

Open FileName For Input As #1

Do While Not EOF©

word = ""
c = Input(1, #1)
If c <> Empty Then
word = word + c
ElseIf Len(word) <> 0 Then
w(i) = word
i = i + 1
Else
word = Empty
End If

Loop
MsgBox word
Close #1
End Sub
Public Sub Open_File()
FileName = "c:\in.txt"
End Sub


One file of unknown length has in each line some words. The number
of words may be different in each line and blank (empty) lines are
allowed. Words may be separated, preceded, and followed, by any
number of spaces. The input file contains spaces and only
alphabetic and numeric characters. It is known in advance that the
number of different words is 100.
Write a VBA program to count the number of each word, it saves in
an output file the result of this count, sorted by the number of
occurrences in decreasing order. When the number of occurrences
is the same for some words, the order of those words is not important.
Optional: if the number of occurrences of some words is the same,
sort them in alphabetical order.

Sample output:

a: 28
at: 11
is: 7
for: 6
the: 6
number: 5
before: 3
count: 2
same: 2
some: 2
very: 2

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}