✔ 最佳答案
欲解卿之難,下兩Functions足矣!
'CountingSpace:計算空白數目
'輸入:strInput:String >要計算空白數目的字串
'輸出:Long >strInput的空白數目
'示範:CountingSpace(“1 2 3 4 5”)
'示範輸出:4
Function CountingSpace(strInput As String) As Long
Dim Space As String
Dim strReplaced As String
Space = " "
strReplaced = Replace$(strInput, Space, vbNullString)
CountingSpace = Len(strInput) - Len(strReplaced)
End Function
'OpenFile:逐行讀取文本
'輸入:FilePath:String >文本之路徑
'輸出:String >文本內容
'示範:OpenFile("C:\WINDOWS\win.ini")
'示範輸出:
'; for 16-bit app support
'[fonts]
'[extensions]
'[mci extensions]
'......
Function OpenFile(FilePath As String) As String
f = freeFile()
On Error GoTo occError
Open FilePath For Input As #f
Do Until EOF(1)
Line Input #f, Data
OpenFile = OpenFile & vbCrLf & Data
Loop
Close #f
OpenFile = Right$(OpenFile, Len(OpenFile) - 2)
Exit Function
occError:
OpenFile = "ERROR TO OPEN THE FILE"
End Function
2008-02-03 03:01:19 補充:
第一,你D Functions Copy左係邊度?Functions應放在全局位置而不是某sub內,例如放在這裡是錯誤的:Private Sub Form_Load()Function xxx() As xx...End FunctionEnd Sub應放在這裡:Private Sub Form_Load()...End SubFunction xxx() As xx...End Function
2008-02-03 03:06:34 補充:
第二,只要用CountingSpace即可計算空白鍵之數目:'CountingSpace函數Function CountingSpace(strInput As String) As Long....'由於字數所限,自己copy返個函數End Function'設某某sub為sss;你的TextBox為tttPrivate Sub sss()Dim 空白鍵數目 As Long空白鍵數目 = CountingSpace(ttt.text)MsgBox 空白鍵數目 '彈個視窗出黎話有幾個空白鍵數目End Sub
2008-02-03 21:55:59 補充:
由於您沒有給出Form(你的表單,Command、Label等的Parent(在沒有container的情況下))、Commad Button、Label及TextBox的Name,所以我且假設:Form的Name是Form1Commad Button的Name是Command1Label的Name是Label1TextBox的Name是Text1程式如下:
2008-02-03 21:59:26 補充:
'(此處無需加上上述之Functions)Private Sub Command1_Click()strInput = Text1.TextDim Space As StringDim strReplaced As StringSpace = " "strReplaced = Replace$(strInput, Space, vbNullString)Label1.Caption = Len(strInput) - Len(strReplaced)End Sub
2008-02-03 22:02:59 補充:
在我這裡,OpenFile是可行的。現假設Form的Name為Form1,程式如下:Function OpenFile(FilePath As String) As String'......由於字數所限,請抄上文的FunctionEnd FunctionPrivate Sub Form_Load()MsgBox OpenFile("C:\WINDOWS\win.ini") '檔案名稱自己改End Sub