VBA错误13:为什么我会出现此错误,尽管它在相同代码下也适用于其他变量

时间:2019-02-15 14:43:15

标签: excel vba types mismatch

我需要过滤一些列(字符串和数字)。我将var定义为变量,将sArray()定义为String。 我在这里变成了错误类型13(仅适用于sArray 4,5):

   ReDim sArray4(1 To UBound(var4))  & ReDim sArray5(1 To UBound(var5))

var4是exp“ Consulting&Supprot”的字符串/变量,但UBound(var4)提供了不匹配项 我不明白var1,var2和var3也像var4 2D变体,并且ReDim sArray2(1 To UBound(var2))...可以正常工作。

任何帮助表示赞赏:)

PS:VBA上的新功能,而且不是我的代码

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

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
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 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
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
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

Workbooks.Open Pfad1
ActiveSheet.Name = ("Quelldatei")
ActiveSheet.Copy After:=ActiveSheet
ActiveSheet.Name = "GMS"
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

1 个答案:

答案 0 :(得分:2)

ReDim sArray4(1 To UBound(var4))

问题是UBound(var4),因为按照以下说明,var4是2D变量数组:

var4 = range4.Value

因为range4包含多个单元格,所以按照以下说明进行操作:

Set range4 = Workbooks(Umsatzdatenbank).Sheets("Filter").Range("C3:C" & lastbez2)

有两种解决方案。

一种方法是在UBound(var4)中指定尺寸:

ReDim sArray4(1 To UBound(var4, 1))

另一种可能是,由于range4是一个单列范围,因此可以使用var4Application.Transpose做成一个单维数组:

var4 = Application.Transpose(range4.Value)

var4为一维数组的情况下,UBound(var4)将按预期工作-请注意,使var4为一维数组将无需为第二个显式指定下标尺寸在这里:

sArray4(i) = var4(i, 1)

...然后保留它会导致下标超出范围错误。