将Range转换为数组时出错

时间:2016-11-18 07:57:10

标签: arrays vba excel-vba excel

有人请帮我解决我的问题吗?

Dim attPresent as Variant ' attpresent()
Set ws = thisworkbook.sheets("Sheet1")
lastrow = ws.cells(Rows.count, 8).end(xlup).row

attPresent = ws.Range("H4:H" & lastrow).Value 'errors if I use Dim attPresent() As Variant

For k = LBound(attPresent, 1) To UBound(attPresent, 1) ' Dim attPresent As Variant'errors if I use         
    msgbox attpresent(k,1)
Next

如果我将变量声明为attPresent = ws.Range("H4:H" & lastrow).Value,则此行Dim attPresent() As Variant会返回错误。然而,如果将变量声明为Dim attPresent As Variant,则此行For k = LBound(attPresent, 1) To UBound(attPresent, 1)会出错。

任何人都可以帮我解决这个问题吗?谢谢

3 个答案:

答案 0 :(得分:3)

作为一种良好做法,请尝试记住使用Option Explicit,并声明所有变量。

当您使用Dim attPresent() As Variant声明数组时,稍后使用attPresent = .Range("H4:H" & lastrow).Value将范围中的值插入数组时,它会自动Redim您的数组为2维数组(1到行号,1到列号)。

Option Explicit

Sub RngtoArray()

Dim attPresent() As Variant  
Dim ws As Worksheet
Dim lastrow As Long
Dim k As Long

Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
    lastrow = .Cells(.Rows.Count, 8).End(xlUp).Row
    attPresent = .Range("H4:H" & lastrow).Value
End With

For k = 1 To UBound(attPresent, 1)
    MsgBox attPresent(k, 1)
Next

End Sub

编辑1 :稍有不同的方法,以防范围内只有1个单元格:

With ws
    lastrow = .Cells(.Rows.Count, 8).End(xlUp).Row

    ' for single column only - create a 1-Dimension array
    ReDim attPresent(1 To lastrow - 4 + 1) ' when the Range starts from "H4"

    For k = 1 To UBound(attPresent)
        attPresent(k) = .Cells(4 + k - 1, "H")
    Next k

End With


For k = 1 To UBound(attPresent)
    MsgBox attPresent(k)
Next

答案 1 :(得分:1)

我尝试将您已定义的内容分开,但为了清楚起见,我认为我提供了完整的代码:

Sub test()

Dim lastrow, i As Integer
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")

Dim attPresent() As Variant

lastrow = ws.Cells(Rows.Count, "H").End(xlUp).Row

ReDim attPresent(lastrow - 4)

For i = 4 To lastrow
    attPresent(i - 4) = ws.Range("H" & i).Value
Next

msg = Join(attPresent, " ")
MsgBox "The array holds: " & vbNewLine & msg

End Sub

我定义了一个没有大小的数组,然后在你知道了lastrow后重新定义它需要的大小(当你从4开始我从lastrow中扣除4时)。< / p>

我猜测msgBox是测试你收集的内容所以我创建了一个转储,将它们全部打印到一个盒子中但显然如果你有很多数据就改变它。的xD

要使用数组,我总是遍历每个单独的条目,一次存储一个。我甚至不确定你是否可以将整个范围一步一步地倾倒,因为我甚至从未研究过它。无论如何,我希望这能解决你的问题kupo。

答案 2 :(得分:1)

Function RangeToArray(rng As Range)
Dim myArray() As Variant, ws As Worksheet
fr = rng.Row
fc = rng.Column
r = rng.Rows.Count
c = rng.Columns.Count
Set ws = rng.Worksheet
ReDim myArray(r - 1, c - 1)
For i = 0 To r - 1
    For j = 0 To c - 1
        myArray(i, j) = ws.Cells(fr + i, fc + j).Value2
    Next j
Next i
RangeToArray = myArray
End Function

Sub f()
Dim rng As Range, attPresent() As Variant ' attpresent()
Set ws = ThisWorkbook.ActiveSheet 'Sheets("Sheet1")
lastrow = ws.Cells(Rows.Count, 8).End(xlUp).Row

Set rng = ws.Range("H4:H" & lastrow)
attPresent = RangeToArray(rng)

For k = LBound(attPresent, 1) To UBound(attPresent, 1) ' Dim attPresent As Variant'errors if I use
    MsgBox attPresent(k, 0)
Next
End Sub

我创建了一个更通用的函数,您也可以在这个特定情况下调用它。