Введите Mismatch VBA при вставке данных в несколько ячеек и использовании цикла - PullRequest
0 голосов
/ 02 апреля 2019

У меня есть раскрывающийся список в ячейках G3:G102, где вы можете выбрать значение от 1 до 50. При выборе числа из списка следующие три столбца заполняются VBA такими вещами, как:

1.
2.
3.

и

On floor 1: ?
On floor 2: ?
On floor 3: ?

Итак, у меня есть этот код VBA.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("G3:G102")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        ' If any one of these cells in the range KeyCells has been modified, then retrieve its value
        floorValue = Range(Target.Address)

        ' Here is the loop I use it will loop X-amount of times based on the number from 'KeyCells'
        Dim i As Integer

        For i = 1 To Range(Target.Address).Value
        ' myText and myText2 are two variables that I am populating here with multiple lines.
            myText = myText & i & "." & vbNewLine
            myText2 = myText2 & "On floor " & i & ": ?" & vbNewLine
        Next i


        'Then insert that data into the cells to the right
        Target.Offset(0, 1).Value = myText
        Target.Offset(0, 2).Value = myText2
        Target.Offset(0, 3).Value = myText2


    End If
End Sub

У меня проблема

Если явставка в несколько ячеек одновременно или перетаскивание данных в несколько ячеек одновременно, я получу ошибку Type mismatch, особенно в этой точке: For i = 1 To Range(Target.Address).Value

Ответы [ 2 ]

1 голос
/ 02 апреля 2019

Я думаю, что, в конечном итоге, вот что вы ищете:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range
    Dim KeyCell As Range
    Dim floorValue As Long
    Dim myText As String
    Dim myText2 As String
    Dim i As Long

    Set KeyCells = Intersect(Me.Range("G3:G102"), Target)

    Application.EnableEvents = False

    If Not KeyCells Is Nothing Then
        For Each KeyCell In KeyCells.Cells
            myText = vbNullString
            myText2 = vbNullString

            ' If any one of these cells in the range KeyCells has been modified, then retrieve its value
            floorValue = KeyCell.Value

            ' Here is the loop I use it will loop X-amount of times based on the number from 'KeyCells'
            For i = 1 To floorValue
                'myText and myText2 are two variables that I am populating here with multiple lines.
                myText = myText & i & "." & vbNewLine
                myText2 = myText2 & "On floor " & i & ": ?" & vbNewLine
            Next i

            'Then insert that data into the cells to the right
            KeyCell.Offset(0, 1).Value = myText
            KeyCell.Offset(0, 2).Value = myText2
            KeyCell.Offset(0, 3).Value = myText2
        Next KeyCell
    End If

    Application.EnableEvents = True

End Sub
0 голосов
/ 02 апреля 2019

Зацикливание каждой ячейки в диапазоне при вставке нескольких значений.Это должно исправить, не проверено.

    Dim rng as Range
    For each rng in Range(Target.Address)
        'myText and myText2 are two variables that I am populating here with multiple lines.
        myText = myText & rng.value & "." & vbNewLine
        myText2 = myText2 & "On floor " & rng.value & ": ?" & vbNewLine
        rng.Offset(0, 1).Value = myText
        rng.Offset(0, 2).Value = myText2
        rng.Offset(0, 3).Value = myText2
    Next
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...