使用For Each循环在数组中添加字符串

时间:2016-10-03 12:32:16

标签: arrays excel vba

我尝试在数组的帮助下在Excel中创建下拉列表。不幸的是,我的代码存在一些问题(我不会显示我的所有代码,我担心它太长了)。

以下是在数组中添加我想要的代码的部分:

Dim Range_Protection As Range
Dim Row_Range As Range
Dim Tableau As Range

Dim Protection_First_Value As String
Dim Protection_Last_Value As String
Dim Array_List() As String

Dim Taille_Array As Integer

If Not Range_Protection Is Nothing Then

'The value I want to get are String, don't know if I should use "Cells.Text" instead
Protection_First_Value = Tableau.Cells(1, 1).Value

    For Each Row_Range In Range_Protection.Rows

        Protection_Last_Value = Row_Range.Cells(1, 1).Value

        'I'm checking the value of each rows
        'Everytime there is a new value, I add it to the Array
        If Protection_First_Value <> Protection_Last_Value Then

            Protection_First_Value = Protection_Last_Value

            'Taille_Array is already determined earlier in the code
            For Count = 0 To Taille_Array

                Array_List(Taille_Array) = Protection_Last_Value

            Next Count
        Else

        End If

    Next Row_Range

End If

创建下拉列表的代码:

With Range("ListeD_Protection").Validation
    .Add Type:=xlValidateList, Formula1:=Join(Array_List, ",")
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True

End With

无论我尝试在同一条线上做什么,我总是有同样的错误:

    .Add Type:=xlValidateList, Formula1:=Join(Array_List, ",")

以下是信息:

  

&#39; 1004&#39;:应用程序定义或对象定义的错误

我在互联网上做过一些研究,但却找不到与我类似的问题。经过几个小时的思考,我已经陷入困境,无法查看错误,即使它在我的代码中某处只是一个小错误。

有人可以告诉我,如果你能理解这是什么问题,我会非常感激。

2 个答案:

答案 0 :(得分:1)

这是因为在Range("ListeD_Protection")中必须有一个验证下拉列表

所以在添加新验证之前添加.Delete

With Range("ListeD_Protection").Validation
    .Delete
    .Add Type:=xlValidateList, Formula1:=Join(Array_List, ",")
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

答案 1 :(得分:1)

我会使用定义的名称来定义一个自动更新的动态范围。

以下是没有代码的方法:

公式:=OFFSET(A1,1,0,COUNTA(A:A)-1,1)

enter image description here

以下是可编程执行的代码:

Worksheets("Sheet1").Range("OFFSET(A1,1,0,COUNTA(A:A)-1,1)").Name = "Accepted_Colors"

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=Accepted_Colors"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With

以下是使用ArrayList重构代码的方法:

Dim Range_Protection As Range
Dim Row_Range As Range
Dim Tableau As Range
Dim ValidationList As String

Dim Array_List As Object

Set Array_List = CreateObject("System.Collections.ArrayList")

Dim Taille_Array As Integer

If Not Range_Protection Is Nothing Then

'The value I want to get are String, don't know if I should use "Cells.Text" instead
Protection_First_Value = Tableau.Cells(1, 1).Value

    For Each Row_Range In Range_Protection.Columns(1)
        If Not Array_List.Contains(Row_Range.Value) Then Array_List.Add Row_Range.Value
    Next Row_Range

    Array_List.Sort

    ValidationList = Join(Array_List.ToArray, ",")
End If