提问:
假设我从Excel表格中复制了一些数据到剪贴板中,比如是这样一些信息:
Allen 12
Anderson 13
Douglas 12
Ohio 49
我怎样才能把这些名字和数字读进一个数组或者一个Grid框中呢?用Clipboard.GetText(
vbCFText)只能一下子读入所有数据,而我希望一个一个地读。
回答:
新建一个项目,在窗体上放两个label和一个command。以下是代码:
Private Sub Command1_Click()
Dim vTekst$
vTekst$ = "Allen 12 "
vTekst$ = vTekst$ & "Anderson 13 "
vTekst$ = vTekst$ & "Bernard 14 "
vTekst$ = vTekst$ & "Constance 15 "
Label1.Caption = vTekst$
Select Case Command1.Caption
Case "Copy Clipboard"
Clipboard.Clear
Clipboard.SetText Label1.Caption
Command1.Caption = "Put into Label"
Case "Put into Label"
Label2.Caption = GetPartofString(Clipboard.GetText, 1)
Command1.Caption = "Copy Clipboard"
''read in array
Dim vText(7) As String
Dim c%
For c% = 0 To 7
vText(c%) = GetPartofString(Clipboard.GetText, c% + 1)
Next c%
''show result
For c% = 0 To 7
MsgBox vText(c%)
Next c%
End Sub
Private Function GetPartofString(source$, part%) As String
Dim p%, c%, tmp$
tmp$ = source$
c% = 0
Do
p% = InStr(tmp, Chr(32))
If p% <> 0 Then
GetPartofString = Left(tmp, p% - 1)
c% = c% + 1
tmp = Right(tmp, Len(tmp) - p%)
End If
Loop While c% <> part%
End Function
--1-------------------------------------------------------------
提问:
我如何才能数出一个字符串中的字母数?举例来说:我想在用户按下OK时计算在Text1.Text中的字母数。
回答:
使用LEN(Text1.text)命令如何?你就可以得到长度……包括空格和其它非字母的字符。所以如果你希望去掉它们的话,必须要一个小函数来检查Text1.Text中的字符是否为真正的字母:
Public Function CountCharacters(source$) As Integer
Dim counter%, t%
Const Characters$ = "abcdefghijklmnopqrstuvwxyz"
For t% = 1 To Len(source$)
If InStr(Characters, LCase$(Mid$(source$, t%, 1))) <> 0 Then
counter% = counter% + 1
End If
Next t%
CountCharacters = counter%
End Function
使用时就象这样:
vString$ = "Testing . about what?"
MsgBox CountCharacters(vString$)
--2-------------------------------------------------------------
提问:
有没有人知道怎样来做这样一个特殊的循环?我需要把一个字符串,比如“Hey how are you”,中的每个字母放到不同的变量中。例如对上面这句话,H放在A1中,e放在A2中,以此类推。谢谢你的帮助。
回答:
Dim vChar() as String
Sub PlaceInArray(source$)
Dim p%
For p% = 1 To Len(source$)
Redim Preserve vChar(p%)
vChar(p%) = Mid$(source$,p%,1)
Next p%
End Sub
在数组vChar的每一项中就分别是给出字符串的所有字母。
--3-------------------------------------------------------------
提问:
你怎样把一个文本文件中的单词一个一个读入字符串变量中?文件中每个单词都被空格分隔。
回答:
Dim vWords() as String
Sub SplitStringintoWords(bron$)
Dim c%, p%,