在Excel单元格中写入

时间:2016-04-21 09:12:02

标签: vba excel-vba excel

我希望我的变量varJoin写在A列的每个单元格中。这样就只能写入1,1 ....我希望有人可以帮助我:))

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

2 个答案:

答案 0 :(得分:0)

首先是因为For c = 0 To ZoneEcrire.Count 中有一个拼写错误:

你已经定义了Range ZoneEc r ,但却在ZoneEcire上循环(没有 r )。

使用Option Explicit可以防止这些错字,但这也会强迫你对所有其他变量进行暗淡。

在它之下,ZoneEcrire是一个范围,因此你必须循环

{{1}}

答案 1 :(得分:0)

感谢您的回答,但我删除了这个:) 它没有必要,最后我的代码看起来像这样:

Sub extractionMots()     Dim Tableau()As String     Dim i As Integer     Dim res As String     Dim ZoneTest作为范围     Dim ZoneEcrire As Range     Dim Cellule选择范围

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

End Sub