VBA错误13:为什么Ubound()中存在不匹配错误?

时间:2019-03-05 15:13:47

标签: excel vba

我需要过滤一些列(字符串和数字)。我将var定义为variant,将sArray()定义为String。我在这里得到sArray (4,5)的错误类型13:

 ReDim sArray4(1 To UBound(var4))
 'and here
 ReDim sArray5(1 To UBound(var5))

var4是exp“咨询与支持”的字符串/变量,但是UBound(var4)给出了不匹配的地方。我不明白var1var2var3也是2D变体,例如var4ReDim sArray2(1 To UBound(var2)) ...可以正常工作。

我已经看到了与此错误有关的问题。我觉得没有帮助

Dim Pfad1 As String
Dim Bezeichnung As Variant
Umsatzdatenbank = ActiveWorkbook.Name
Pfad1 = Workbooks(Umsatzdatenbank).Sheets("Umsatz").Cells(5, 5)

Dim range1, range2, range3, range4, range5 As Range
lastoffice = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("H" & Rows.Count).End(xlUp).Row
lastoffice = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("H" & Rows.Count).End(xlUp).Row
lastbez = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("B" & Rows.Count).End(xlUp).Row
lastbez2 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("C" & Rows.Count).End(xlUp).Row
lastoffice2 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("F" & Rows.Count).End(xlUp).Row

Set range1 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("B3:B" & lastbez)
Set range2 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("E3:E" & lastoffice)
Set range3 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("H3:H" & lastoffice)
Set range4 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("C3:C" & lastbez2)
Set range5 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("F3:F" & lastoffice)

Dim var1, var2, var3, var4, var5 As Variant
Dim sArray1(), sArray2(), sArray3(), sArray4(), sArray5()  As String
Dim i As Long

'Bestimmung des Arrays    
var1 = range1.Value
ReDim sArray1(1 To UBound(var1))
For i = 1 To (UBound(var1))
    sArray1(i) = var1(i, 1)
Next

var2 = range2.Value
ReDim sArray2(1 To UBound(var2))
For i = 1 To (UBound(var2))
    sArray2(i) = var2(i, 1)
Next

var3 = range3.Value
ReDim sArray3(1 To UBound(var3))
For i = 1 To (UBound(var3))
    sArray3(i) = var3(i, 1)
Next

var4 = range4.Value
ReDim sArray4(1 To UBound(var4))

For i = 1 To (UBound(var4))
    sArray4(i) = var4(i, 1)
Next

var5 = range5.Value
ReDim sArray5(1 To UBound(var5))

For i = 1 To (UBound(var5))
    sArray5(i) = var5(i, 1)
Next

Workbooks.Open Pfad1
ActiveSheet.Name = ("Quelldatei")
ActiveSheet.Copy After:=ActiveSheet
ActiveSheet.Name = "FSS"
LastRow = Range("A" & Rows.Count).End(xlUp).Row   

Range("K1").Select
Selection.AutoFilter
Worksheets("FSS").Range("$A$1:$AA$" & LastRow).AutoFilter Field:=11, Criteria1:=sArray1, Operator:=xlFilterValues
Worksheets("FSS").Range("$A$2:$AA$" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData
Worksheets("FSS").Range("$A$1:$AA$" & LastRow).AutoFilter Field:=13, Criteria1:=sArray2, Operator:=xlFilterValues
Worksheets("FSS").Range("$A$2:$AA$" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData

'Sheets("FSS").Range("$A$1:$AA$" & lastRow).AutoFilter Field:=18, Criteria1:=sArray3, Operator:=xlFilterValues

Workbooks.Open Pfad1
ActiveSheet.Name = ("Quelldatei")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Name = "GMS"

Range("K1").Select
Selection.AutoFilter
Worksheets("Quelldatei").Range("$A$1:$AA$" & LastRow).AutoFilter Field:=11, Criteria1:=sArray4, Operator:=xlFilterValues
Worksheets("Quelldatei").Range("$A$1:$AA$" & LastRow).AutoFilter Field:=13, Criteria1:=sArray5, Operator:=xlFilterValues
Worksheets("Quelldatei").Range("$A$2:$AA$" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy
Worksheets("GMS").Range("$A$2:$AA$" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Paste
ActiveSheet.ShowAllData

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

您正在使用varX = rangeX.Value从工作表中读取值。如果Range包含多个单元格,则结果为二维数组。但是,如果“范围”仅包含一个单元格,则结果是单个值,而不是数组-您不能对单个(标量)值使用UBound函数。

现在,对于过滤器,您可以传递单个值或一维数组。该代码将范围数据的第一列复制到新的一维变量sArrayX

对于两种情况,您只有一个值,而对于多个值则必须区分。当您完成5次整个工作时,我建议为此使用一个函数:

 Function copyToArray(r As Range) As Variant
    Dim a As Variant
    If r.Count = 1 Then
        ReDim a(1 To 1)
        a(1) = r.Value2
    Else
        ReDim a(1 To r.Rows.Count)
        Dim i As Long
        For i = 1 To r.Rows.Count
            a(i) = r.Cells(i, 1).Value2
        Next i
    End If
    copyToArray = a

End Function

您可以这样调用该函数:

Dim sArray1, sArray2, sArray3, sArray4, sArray5 ' Declare as Variant is okay!
sArray1 = copyToArray(range1)
sArray2 = copyToArray(range2)
...

不再需要VarX变量。备注:类型Variant的变量可以容纳任何内容,其中包括数组。使用变体将数组从一个函数传递到一个函数,通常是最简单的解决方案。

我强烈建议您为变量赋予更多描述性名称。