Assuming you know how to open up your vba editor so I'll skip that part.
Create a subroutine and call it whatever you want, we'll call it test for a visual explanation.
Then create a variable to attach to the sheet that you want, lets say the first sheet.
Then you can access the cells of the worksheet through the Cells(row, column) attribute.
To open a file for reading use the open keyword.
Use line input to get read the file line by line and once you encounter the word that you are looking for start recording the contents of the file into the worksheet. The text that is in red is that which will depend on you. For the most part, this is the basic logic that you need for the code.
Code:- Private Sub
- test()
- Dim
- theSheet
- As
- Worksheet
- Dim
- iFile
- As Integer
- Dim
- sLine
- As String
- Dim
- bRecord
- As Boolean
- Dim
- lRowCntr
- As Long
- 'set theSheet equal to the sheet
- Set
- theSheet = Application.ActiveWorkbook.Sheets(1)
- 'get the next free file handle
- iFile = FreeFile
- lRowCntr = 1
- 'open the file
- Open
- "
- yourfile.txt
- "
- For Input As
- #iFile
- While Not
- EOF(iFile)
- Line Input
- #iFile, sLine
- If
- Trim(sLine) = "
- your phrase
- "
- Then
- bRecord =
- True
- If
- bRecord
- Then
- theSheet.Cells(lRowCntr, 1) = Trim(sLine)
- lRowCntr = lRowCntr + 1
- End If
- Wend
- Close
- #iFile
- End Sub
Copy Code
I need to be able to organize all of those numbers into one column (left to right across the row, and then down to the next row, in that order) in an Excel worksheet. Can anyone help me out? Thanks. |