【发布时间】:2019-02-14 15:23:43
【问题描述】:
我正在尝试创建一个 Msgbox,用于显示工作表上一列中的所有单词。然后创建另一个消息框,其中包含单词数以及大于 5 个字符的单词。我不太确定如何使用以下变量创建 Msgbox,因此我将不胜感激!
到目前为止,我编写了一个子程序,它使用 GetOpenFilename 从文本文件中导入单行文本。然后我使用 split 使用分隔符解析文件。
文本文件内容如下:
Zags、Golden Eagles、Hawks、Peacocks、Greyhounds、Golden Griffins、Dons、Musketeers
以下是我目前的代码:
Sub ImportTextFile()
Dim fileName As Variant ' array will hold the file name or names to import
Dim i As Integer ' counter for loops
Dim nLargeWords As Integer ' counter for words longer than 5 characters
Dim dataLine As String ' string to hold a single line of text from file
Dim lineCount As Integer
Dim arrWords() As String ' array that will hold the contents of the text file
Dim msg As String ' string for building the first message box
Const delim = "," ' Added a constant for the delimiter in Split function
With Range("Output_lbl")
Range(.Offset(1, 1), .Offset(100, 2)).ClearContents
End With
'============Navigate to file location and get filename of text file
fileName = Application.GetOpenFilename(, , "Select a text file", , False) ' Used GetOpenFilename method to navigate to the file and obtain the file name.
'============Import file into temp string variable then parse into an array
Open fileName For Input As #1 ' To open the text file.
i = 0
With Range("Output_lbl")
Do Until EOF(1)
Line Input #1, dataLine ' Setting the first line of the text file to a string variable.
arrWords() = Split(dataLine, delim) ' Using Split to parse the file. The delimiter was determined to be "," which was declared a constant above.
For i = LBound(arrWords()) To UBound(arrWords()) ' Parse into an array.
.Offset(i, 0) = Trim(arrWords(i))
Next i
i = i + 1
Loop
End With
Close #1 ' To close the text file.
'============Create and display the message boxes and output array contents and word lengths to the output sheet.
End Sub
一个消息框的格式应该是这样的:
文件中的单词。
Zags
Golden Eagles
Hawks
Peacocks
Greyhounds
Golden Griffins
Dons
Musketeers
另一个消息框的格式应该是这样的:
Microsoft Excel
Total Words: 8
Large Words: 5
【问题讨论】: