Копирование VBA ComboBox иногда происходит сбой - PullRequest
0 голосов
/ 12 марта 2019

Я создаю форму с 3 выпадающими списками, которые пользователь может заполнить.Когда они нажимают кнопку «Новая строка», форма должна создать новую строку непосредственно под самой последней строкой с 3 пустыми выпадающими списками.

ПРОБЛЕМА: Иногда, когда пользователь выбирает один из полей не по порядку(например, они нажимают combobox 2 перед combobox1), а затем они нажимают кнопку «новая строка», вторые два поля копируются правильно, но первый combobox не копируется в правильную строку.Он всегда копируется в верхний левый ряд таблицы.(См. Рисунок)

Правильно:

Correct result

Неверно:

Incorrect result

Вот мой код:

'Declare variables
Dim curIndirectRow As Integer

'Search for keyword in column B; search direction: top to bottom
'Set findTable to the coordinates of matching word
Set findTable = ThisWorkbook.Sheets("Main").Range("B:B").Find(What:="Indirect Damages", LookIn:=xlValues, LookAt:=xlWhole, searchdirection:=xlNext)

'If the line immediately after the found word is blank (meaning it's the first row)
If Range(findTable.Address).Offset(1, 0) = "" Then
    'set variable to be the first row
    curIndirectRow = Range(findTable.Address).Row
    Else
    'set variable to be the last row in the table
    curIndirectRow = ThisWorkbook.Sheets("Main").Range(findTable.Address).End(xlDown).Row
End If

'Add one blank row underneath your last row
Range("$B$" & curIndirectRow).Offset(1).EntireRow.Insert Shift:=xlShiftDown
'Copy the row above it into your newly inserted row(no buttons included)
Sheets("Main").Range("B" & CStr(findTable.Row) & ":F" & CStr(findTable.Row)).Copy Destination:=Sheets("Main").Range("B" & CStr(curIndirectRow + 1) & ":F" & CStr(curIndirectRow + 1))
'Clear the values of the Approver cell
Sheets("Main").Range("$F$" & CStr(curIndirectRow + 1)).Value = ""

'Select the first box ("Type") and copy
Sheets("Main").Shapes.Range(Array("Combobox8")).Select
Selection.Copy
'Paste the first box into your new row's C column
Sheets("Main").Range("C" & CStr(curIndirectRow + 1)).PasteSpecial
'Populate the drop down list
Sheets("Main").Shapes("ComboBox" & CStr(Sheets("Main").Shapes.Count - 63)).OLEFormat.Object.ListFillRange = "=Lists!D2:D6"
'Set the default value to blank
Sheets("Main").OLEObjects("ComboBox" & CStr(Sheets("Main").Shapes.Count - 63)).Object.Value = ""

Код для первого блока повторяется для второго и третьего блоков.Обратите внимание, что я вычел 63 из числа фигур на листе, потому что есть еще 63 фигуры (флажки и т. Д.), Которые не являются комбинированными.

...