2016-04-21 136 views
0

我想我的變量varJoin寫在A列的每個細胞這樣寫只是在1,1 ....我希望有人能幫助我:)寫在Excel單元格中

Sub extractionMots() 
    Dim Tableau() As String 
    Dim i As Integer 
    Dim res As String 
    Dim ZoneTest As Range 
    Dim ZoneEcrire As Range 
    Dim CelluleSelect As Range 

    Dim arr As Variant 
    Dim varJoin As Variant 

    Set ZoneTest = Range("C1:C16") 
    Set ZoneEcrire = Range("A1:A16") 

     For Each CelluleSelect In ZoneTest 

      ' Suppression des espaces superflus 
      CelluleSelect = Application.WorksheetFunction.Trim(CelluleSelect) 
      res = CelluleSelect.Value 

      Debug.Print res 
      i = 0 
      Tableau() = Split(res, ".") 'découpe la chaine en fonction des points " " 
      'x = Tableau(i) 'le résultat de la fonction Split est stocké dans un tableau 

      For i = 0 To UBound(Tableau) 

       x = Tableau(1) 
       A = Tableau(0) 
       b = Tableau(2) 

       For j = 0 To Len(x) 

        Do While Len(x) < 6 
         x = "0" + x 
        Loop 

        Do While Len(b) < 4 
         b = "0" + b 
        Loop 

       Next j 

      Next i 

     Debug.Print x 

     For c = 0 To Len(ZoneEcire) 

      'define array: 
      arr = Array(A, x, b) 

      'using the vba Join function to join substrings contained in an array: 
        varJoin = Join(arr, ".") 

      'return string after joining the substrings: 
      Cells(c + 1, 1).Value = varJoin 
     Next c 
    Next 

End Sub 

回答

0

首先it'是因爲你For c ... loop看到一隻錯字:

......你所定義的範圍ZoneEc [R憤怒,但遍歷ZoneEcire(不[R)。

使用Option Explicit可以防止這些錯字,但是這會迫使您將所有其他變量也設爲Dim。

在其下方,ZoneEcrire是一個範圍,因此,你必須循環

For c = 0 To ZoneEcrire.Count 
0

謝謝你的答案,但我刪除對於:) 這是沒有必要的,最後我的代碼如下所示:

子extractionMots() 昏暗的Tableau()作爲字符串 昏暗我作爲整數 昏暗RES作爲字符串 昏暗ZoneTest作爲範圍 昏暗ZoneEcrire作爲範圍 昏暗CelluleS當選爲靶場

Dim arr As Variant 
Dim varJoin As Variant 

Set ZoneTest = Range("C1:C16") 
Set ZoneEcrire = Range("A1:A16") 

For Each CelluleSelect In ZoneTest 

    ' Suppression des espaces superflus 
    CelluleSelect = Application.WorksheetFunction.Trim(CelluleSelect) 
    res = CelluleSelect.Value 

    Debug.Print res 
    i = 0 
    Tableau() = Split(res, ".") 'découpe la chaine en fonction des points " " 
    'x = Tableau(i) 'le résultat de la fonction Split est stocké dans un tableau 

    For i = 0 To UBound(Tableau) 

     x = Tableau(1) 
     A = Tableau(0) 
     b = Tableau(2) 

     For j = 0 To Len(x) 

      Do While Len(x) < 6 
       x = "0" + x 
      Loop 

      Do While Len(b) < 4 
       b = "0" + b 
      Loop 

      'define array: 
      arr = Array(A, x, b) 
      'using the vba Join function to join substrings contained in an array: 
      varJoin = Join(arr, ".") 

     Next j 
    Next i 

    'return string after joining the substrings:' 
    Cells(c + 1, 1).Value = varJoin 
    c = c + 1 
Next 

末次