如何解决每次获取之前的值
Dim txt As String
Dim i As Integer
Dim reference As Variant
Dim d As Integer
d = Worksheets("Sheet1").cells(Rows.Count,"a").End(xlUp).Row
txt = cells(3,4).Value
reference = Split(txt," ")
For i = 0 To UBound(reference)
cells(d + 1,[4]).Value = reference(i)
Next
txt = cells(3," ")
cells(d + 1,[12]).Value = reference(3)
嗨,我试图每次在ubound值之前选择引用,然后复制到最后一行的引用。当它是字符串的第4部分时,我得到了此代码,但是我试图始终选择ubound之前的值。是否可以做UBOUND -1。还是我必须另辟way径。谢谢麦克斯
解决方法
基本上有2种方法选择优先值。
选项1 -使用Ubound()
:
Sub TestMe()
Dim reference As String
reference = "Stack Overflow is my favourite VBA site!"
Dim splitted As Variant
splitted = Split(reference)
Debug.Print splitted(UBound(splitted) - 1)
End Sub
选项2 -使用预定义的函数获取数组长度并从中删除2:
这样调用:
Debug.Print splitted(GetArrayLength(splitted) - 2)
功能:
Private Function GetArrayLength(myArray As Variant) As Long
If IsEmpty(myArray) Then
GetArrayLength = 0
Else
GetArrayLength = UBound(myArray) - LBound(myArray) + 1
End If
End Function
该功能要好一些,因为它可以检查空数组。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。