我想从Excel中的标记工作表中捕获数组中的值,并在下一个工作表中返回值。我试图使数组动态化。我编写的代码如下:
Private Sub CommandButton1_Click()
Dim myarr() As String
Dim lrow As Integer, lc As Integer, r As Integer, c As Integer
lrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
lc = Sheets("Sheet1").Range("A1").End(xlToRight).Column
ReDim myarr(lrow, lc) As String
For r = 1 To lrow
For c = 1 To lc
myarr(r, c) = Cells(r, c).Value
Next c
Next r
Sheets("Sheet2").Select
For r = 1 To lrow
For c = 1 To lc
Cells(r, c) = myarr(r, c)
Next c
Next r
End Sub
我期望数组返回表2中的值,但没有得到结果。它没有发出错误。
1条答案
按热度按时间bvuwiixz1#
在可能的情况下,用特定的工作表引用限定范围,不要依赖Select。我希望在运行代码时选择了工作表2,因此第一个
Cells(r, c).Value
将引用该工作表。或者更简单地说