VBA错误13:为什么我会成为同一代码的其他差异,尽管我会成为此错误



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

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

var4是Exp"咨询& splrot"的字符串/变体,但是Ubound(var4)交付了一个MISSMATCH我不明白var1,var2和var3也类似于var4 2d variant和redim sarray2(1到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
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是单列范围,它使用Application.Transpose

使var4成为单一数组
var4 = Application.Transpose(range4.Value)

var4是单维数组,UBound(var4)将按预期工作 - 请注意,使var4成为单维数组删除了在此处明确指定下标:

明确指定下标:
sArray4(i) = var4(i, 1)

...并将其放入将导致A subscript脱离范围错误。

相关内容

  • 没有找到相关文章

最新更新