我有264列数据,我需要实际地将每个数据设置为一个范围,以将值存储在变量中...所以我认为

For i = 1 To 5

If i = 1 Then
    search1 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 2 Then
    search2 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 3 Then
    search3 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 4 Then
    search4 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
ElseIf i = 5 Then
    search5 = Sheets(tables).Range(Cells(2, i), Cells(200, i))
End if

Next
.

(我有264我,所以我认为这样做不好......) 我怎样才能做到这一点 ?

我已经尝试过:

search & i = Sheets(tables).Range(Cells(2, i), Cells(200, i))
"Compiler error, expected expression"
search(i) = Sheets(tables).Range(Cells(2,i),Cells(200,i))
"Sub or Function not Defined"
.

仍然不起作用。

是可能的吗?你们有什么暗示吗?这样做的技巧?

请告诉我任何想法,我将提供尽可能多的信息来这里至少有一个很好的解决方案。

谢谢。

有帮助吗?

解决方案

尝试此代码:

Sub test()
    Dim i As Integer, n As Integer
    Dim tables As String
    Dim search() As Variant

    n = 264
    ReDim search(1 To n)

    tables = "Sheet1"

    With ThisWorkbook.Sheets(tables)
        For i = 1 To n
            search(i) = .Range(.Cells(2, i), .Cells(200, i)).Value
        Next
    End With

End Sub
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top