簡體   English   中英

在Excel單元格中寫入

[英]Write in Excel cell

我希望我的變量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

首先是因為For c ... loop錯別字:

您已經定義了Range ZoneEc r ire,但是正在ZoneEcire上循環(沒有r )。

使用Option Explicit可以防止出現這些錯字,但這也迫使您也要使所有其他變量變暗。

在此之下,ZoneEcrire是一個Range,因此您必須循環

For c = 0 To ZoneEcrire.Count

謝謝您的回答,但是我刪除了For :),這是沒有必要的,最后我的代碼如下所示:

子提取Mots()Dim Tableau()作為字符串Dim i作為整數Dim res作為字符串Dim ZoneTest作為范圍Dim ZoneEcrire作為Range Dim CelluleSelect作為范圍

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

結束子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM