2012-01-22 20 views
0

正規表現検索の結果を文字列の配列に送るために、以下のコードを取得しようとしています。どうやってやるの?文字列の配列に正規表現の一致を送信する

名前を文字列の配列に変更すると、Dim name() as String VBAは型の不一致の例外をスローします。私はそれを修正するために何ができるのか?

多くのありがとうございます。

Do While Not EOF(1) 
    Line Input #1, sText 
    If sText <> "" Then 


     Dim Regex As Object, myMatches As Object 

     ' instantiates regexp object 
     Set Regex = CreateObject("VBScript.RegExp") 
     With Regex 
      .MultiLine = False 
      .Global = True 
      .IgnoreCase = False 
      .Pattern = "^Personal\sname\s*[:]\s*" 
     End With 

     ' get name, seperated from Personal Name 
     If Regex.test(sText) Then 

      Set myMatches = Regex.Execute(sText) 
      Dim temp As String 
      temp = Regex.Replace(sText, vbNullString) 
      Regex.Pattern = "^[^*]*[*]+" 
      Set myMatches = Regex.Execute(temp) 
      Dim temp2 As String 
      temp2 = myMatches.Item(0) 
      name = Trim(Left(temp2, Len(temp2) - 3)) 

     End If 
    End If 
Loop 

答えて

3

「名前」は、excelプロパティと競合するため、変数名として使用しないでください。代わりにsNameまたはsNamesを試してください。ここでsは文字列です。

アレイでは、各要素に値を割り当てる前にサイズを指定する必要があります。

Dim sNames(4) As String '// Or Dim sNames(1 To 4) As String 

sName(1) = "John" 
... 
sName(4) = "Sam" 

か、その後で開始する要素(名前)の合計数がわからない場合:

Dim sNames() As String 
    Dim iTotalNames As Integer 

    '// Your code .... 

    iTotalNames = iTotalNames + 1 
    ReDim Preserve sNames(iTotalNames) 
    sNames(iTotalNames) = Trim(Left(temp2, Len(temp2) - 3)) 

    '// Rest of your code ... 

Dim sNames() As String 
Dim iTotalNames As Integer 

iTotalNames = '// Some code here to determine how many names you will have 

ReDim sNames(iTotalNames) '// You can also use ReDim Preserve if you have existing elements 

sName(1) = "John" 
... 
sName(4) = "Sam" 

は、だから私はあなたのようなものが必要になります疑いまた、VBAでは、変数のすべての寸法はモジュールの最上部にある必要があります。

0

変更

'call this "A" 
Dim temp2 As String 
temp2 = myMatches.Item(0) 

'stick this at the top 
redim temp2(0 to 0) 

'replace "A" with this 
new_top = ubound(temp2)+1 
redim preserve temp2 (0 to new_top) 
temp2(new_top) = myMatches.Item(0) 
関連する問題