Словарь пуст после объявления и заполнения - PullRequest
0 голосов
/ 03 мая 2020

Я не очень хорошо разбираюсь в VBA, поэтому я подозреваю проблему с объявлением и последующим использованием словаря.

Я выбрал другой подход. Созданы две функции для создания диктов.

Сначала for l oop проверяет, является ли элемент управления в пользовательской форме текстовым полем, затем получает номер столбца (dict_col) и проверяет, нужно ли форматировать его как дату (dict_for).

Однако каждый раз, когда второй dict кажется пустым ... Когда я проверяю содержимое каждого dict отдельно (до l oop), он показывает правильные значения.

Public Function import_columns(rng As Variant) As Dictionary
    Dim dict As New Dictionary
    Dim i As Long
    Dim count_rows As Long
    Dim dict_k As String, dict_i As String

    count_rows = rng.Rows.Count
    For i = 1 To count_rows
        dict_k = rng(i, 2)
        dict_i = rng(i, 1)
        dict.Add dict_k, dict_i
    Next i

    Set import_columns = dict

End Function

Public Function import_format(rng As Variant) As Dictionary
    Dim dict_f As New Dictionary
    Dim i As Long
    Dim count_rows As Long

    count_rows = rng.Rows.Count
    For i = 1 To count_rows
        dict_f(rng(i, 1)) = 0
    Next i

    Set import_format = dict_f

End Function

Private Sub UserForm_Initialize()
'On Error GoTo ErrorHandle

Dim wb As Workbook
Dim rng_col As Range
Dim rng_format As Range
Dim dc_value As Integer
Dim ctrl As Control
Dim ctrlType As String
Dim ctrl_name As String
Dim key As Variant


Dim dict_col As Dictionary
Dim dict_for As Dictionary



Set rng_col = Application.Union(Range("columns_mark").Columns(3), Range("columns_mark").Columns(2))
Set rng_format = Arkusz25.Range("H1").CurrentRegion

Set dict_col = import_columns(rng_col)
Set dict_for = import_format(rng_format)

'Me.Results.Enabled = False


ListBox1.RowSource = "lista"
txt_results = ListBox1.ListCount

For Each key In dict_col.Keys
    'If dict_col.Exists(key) Then
        Debug.Print key
        Debug.Print dict_col(key)
    'End If
Next key


ctrlType = "TextBox"
For Each ctrl In Results.Controls
    ctrl_name = ctrl.Name
    If TypeName(ctrl) = ctrlType Then
        dc_value = dict_col(ctrl_name)
        If dict_for.Exists(ctrl_name) Then
                ctrl = Format(Val(ListBox1.List(0, dc_value - 1)), "dd.mm.yyyy")
            Else
                ctrl = ListBox1.List(0, dc_value - 1)
            End If
        ctrl.Enabled = False
    End If
Next ctrl
...