英文:
Mismatch error 13 in Ubound when entering a single value
问题
我的代码按照我的预期工作,但当我需要输入单个值时,它显示错误13,我真的不知道为什么。我认为这是因为你不能在单个值数组中使用ubound,但我不知道如何解决或替换它。
我尝试使用一个If语句,当我有一个单一值时,它只是使用另一个变量,但它没有起作用,我真的不认为这是一个解决方案。
Dim material As Variant, cantidad As Variant
Dim g As Long, lastrow As Long, cant As Long
Dim wsCarg As Worksheet, wsRem As Worksheet, wb As Workbook, wsDev As Worksheet
Set wb = ThisWorkbook '例如
Set wsCarg = wb.Worksheets("CARGA")
Set wsRem = wb.Worksheets("REMITO")
Set wsDev = wb.Worksheets("DEVOLUCIONES")
lastrow = wsRem.Cells(Rows.Count, "C").End(xlUp).Row
material = wsRem.Range("C28:C" & lastrow).Value
cantidad = wsRem.Range("M28:M" & lastrow).Value
g = UBound(material, 1) '数据行数
这是代码中出现错误的部分,如果你有任何建议,我将不胜感激,对不起我的英文,提前谢谢!
英文:
My code works how I want it to work, but when I have to put a single value, it shows error 13, and I don't really know why. I think it is because you can't use ubound in a single value array, but I don't know how to solve or replace it
I tried using an If that when I have a single value it just uses another variable, but it didn't work, and I don't really think that's a solution.
Dim material As Variant, cantidad As Variant
Dim g As Long, lastrow As Long, cant As Long
Dim wsCarg As Worksheet, wsRem As Worksheet, wb As Workbook, wsDev As Worksheet
Set wb = ThisWorkbook 'for example
Set wsCarg = wb.Worksheets("CARGA")
Set wsRem = wb.Worksheets("REMITO")
Set wsDev = wb.Worksheets("DEVOLUCIONES")
lastrow = wsRem.Cells(Rows.Count, "C").End(xlUp).Row
material = wsRem.Range("C28:C" & lastrow).Value
cantidad = wsRem.Range("M28:M" & lastrow).Value
g = UBound(material, 1) 'number of rows of data
This is the part of the code which has an error, if you can suggest anything I will appreciate it, sorry for my English, and thanks in advance!
答案1
得分: 3
Range.Value
总是返回一个包含值的二维数组(即使您只有一行或一列的数据)。除非范围只包含一个单元格,在这种情况下,它将简单地返回该值本身(而不是数组)。
有许多方法来检查这一点:
使用范围的属性Count
(或更好地使用CountLarge
以避免溢出)。请注意,这只有在您只有一列时才能正常工作,因为它会计算范围的单元格总数(行 * 列)
g = wsRem.Range("C28:C" & lastrow).CountLarge
您可以使用范围的Rows.Count
属性
g = wsRem.Range("C28:C" & lastrow).Rows.Count
您可以将lastrow
与起始行进行比较。我建议使用一个常量定义:
Const startRow = 28
material = wsRem.Range("C" & startRow & ":C" & lastrow).Value
g = lastRow - startRow + 1
您可以检查返回的值是否是数组:
If IsArray(material) Then
g = UBound(material, 1)
Else
g = 1
End If
然而,当您处理数据(例如material
)时,如果它只包含一个值,将其转换为具有1*1元素的二维数组可能会很方便。这样,您在后续代码中无论处理数组还是非数组都不必担心,例如:
If Not IsArray(material) Then
Dim tmpVal As Variant
tmpVal = material
ReDim material(1 To 1, 1 To 1)
material(1, 1) = tmpVal
End If
英文:
Range.Value
returns always a 2-dimensional array with the values (even if you have only one row or one column of data). Except if the range contains only one cell, in that case it simply returns the value itself (not as array).
There are many ways to check this:
Use the property Count
(or, even better CountLarge
to avoid overflows) of the range. Note that this will work only correctly if you have only one Column as it counts the total number of cells of the range (rows * columns)
g = wsRem.Range("C28:C" & lastrow).CountLarge
You can use the Rows.Count
-property of the Range
g = wsRem.Range("C28:C" & lastrow).Rows.Count
You could compare lastrow
with the starting row. I would suggest to use a constant definition:
Const startRow = 28
material = wsRem.Range("C" & startRow & ":C" & lastrow).Value
g = lastRow - startRow + 1
You can check if the returned value is an array:
If IsArray(material) Then
g = UBound(material, 1)
Else
g = 1
End If
However, when you process the data (eg material
), it might come handy to convert it to a 2-dimensional array with 1*1 elements when it contains only a single value. With that, you don't have to take care if you deal with an array or not in the following code, eg
If Not isArray(material) Then
Dim tmpVal As Variant
tmpVal = material
Redim material(1 to 1, 1 to 1)
material(1, 1) = tmpVal
End If
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论