【问题标题】:Populating a Range parameter to array using a string variable使用字符串变量将 Range 参数填充到数组
【发布时间】:2021-01-23 08:31:28
【问题描述】:

我正在尝试从该行中已更改的单元格中选择一行,然后从选定的行值中填充一个数组。

当我尝试构建一个包含所选行(如 Copyrange = Start Position & End Position)的字符串变量,然后将其提供给 Range 参数到数组中时,它会引发下标超出范围错误。

我需要制定正确的行来分配给 Range,如果不能将字符串变量传递给 Range 参数,我该怎么办?

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r, s As Long
  
    
    If Not Application.Intersect(Target, Range("PrioritySelect")) Is Nothing Then
    If Not Application.Intersect(Target, Range("PrioritySelect")) = "Select" Then
        r = Target.Row
        s = r + 8
        ' MsgBox "row " & r, , "Amended Cell..."
        
        Dim MyArray() As Variant
        
        'unallocated array
        Dim i As Integer
        Dim ThisWs As Worksheet
        Dim Copyrange As String
        Dim Position As String
        
        
        Set ThisWs = Worksheets("PIPPR")
        Position = "B" & r
        EndPosition = "B" & s
           MsgBox Position
            MsgBox EndPosition
            
        Let Copyrange = Position & ":" & EndPosition
        MsgBox Copyrange
                    
        MyArray = ThisWs.Range(Copyrange).Value2
        
        MsgBox "Lower Bound = " & LBound(MyArray)
        MsgBox "Upper Bound = " & UBound(MyArray)
        MsgBox MyArray(1)
        
        'MsgBox MyArray(1)
        
        
            ' Print student marks from the array to the Immediate Window
    Debug.Print "Values"
    For i = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(i)
    Next i

【问题讨论】:

  • 尝试使用Copyrange = Position & ":" & EndPosition 而不是Let Copyrange = Position & ":" & EndPosition。字符串必须以这种简单的方式接收值。
  • ThisWs.Range(Copyrange) cannot give a subscript out of range 错误。你得到它的地方是MyArray(1)MyArray 是二维数组,不是一维数组。

标签: arrays excel vba string range


【解决方案1】:

MyArray 是一个二维数组。你必须正确使用它。试试这个:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r, s As Long
  
    
    If Not Application.Intersect(Target, Range("PrioritySelect")) Is Nothing Then
    If Not Application.Intersect(Target, Range("PrioritySelect")) = "Select" Then
        r = Target.Row
        s = r + 8
        ' MsgBox "row " & r, , "Amended Cell..."
        
        Dim MyArray() As Variant
        
        'unallocated array
        Dim i As Integer
        Dim ThisWs As Worksheet
        Dim Copyrange As String
        Dim Position As String
        
        Set ThisWs = Worksheets("PIPPR")
        Position = "B" & r
        EndPosition = "B" & s
           MsgBox Position
            MsgBox EndPosition
            
        Let Copyrange = Position & ":" & EndPosition
        MsgBox Copyrange
                            
        MyArray = ThisWs.Range(Copyrange).Value2
        
        MsgBox "Lower Bound = " & LBound(MyArray)
        MsgBox "Upper Bound = " & UBound(MyArray)
        MsgBox MyArray(1, 1)
        
        'MsgBox MyArray(1)
        
        
            ' Print student marks from the array to the Immediate Window
    Debug.Print "Values"
    For i = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(i, 1)
    Next i
    End If
    End If
End Sub

这个使用带有字符串的一维 MyArray 代替或变体:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r, s As Long
  
    
    If Not Application.Intersect(Target, Range("PrioritySelect")) Is Nothing Then
    If Not Application.Intersect(Target, Range("PrioritySelect")) = "Select" Then
        r = Target.Row
        s = r + 8
        ' MsgBox "row " & r, , "Amended Cell..."
        
        'unallocated array
        Dim i As Integer
        Dim ThisWs As Worksheet
        Dim Copyrange As String
        Dim Position As String
        Dim Cell As Range
        Dim MyArray() As String
        
        Set ThisWs = Worksheets("PIPPR")
        Position = "B" & r
        EndPosition = "B" & s
           MsgBox Position
            MsgBox EndPosition
            
        Let Copyrange = Position & ":" & EndPosition
        MsgBox Copyrange
        
        ReDim MyArray(1 To ThisWs.Range(Copyrange).Cells.Count)
        i = 1
        For Each Cell In ThisWs.Range(Copyrange)
            MyArray(i) = Cell.Value2
            i = i + 1
        Next
        
        MsgBox "Lower Bound = " & LBound(MyArray)
        MsgBox "Upper Bound = " & UBound(MyArray)
        MsgBox MyArray(1)
        
        'MsgBox MyArray(1)
        
        
            ' Print student marks from the array to the Immediate Window
    Debug.Print "Values"
    For i = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(i)
    Next i
    End If
    End If
End Sub

【讨论】:

  • 哦,太有帮助了,感谢邪恶的蓝猴!这样可行。有没有办法将它输入一维数组,因此没有这个问题?
  • 我似乎也无法访问 i 大于 2 的数组值 - Debug.Print 只打印出总共 9 的前 2 个值,这很奇怪。
  • @JamesintheUK 查看LBound() function 文档及其dimension 参数
  • 对不起,我不是你们都知道的世界上最伟大的程序员。我卡住的一点是我不明白数组是如何形成的。我成功的范围包含 9 个值,但是这 9 个似乎不会导致在数组中创建 9 个值,只有 2 个?据我所知。
  • 我在答案中添加了一个使用一维 MyArray 的额外代码。我还把它变成了字符串类型而不是变体;因为我们只需要复制单元格的值,所以不需要使用变体。
猜你喜欢
  • 2021-01-26
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多