获取范围内单元格的值,将其存储为变量,然后存储下一行单元格,下一行(Excel)

时间:2016-02-10 14:44:35

标签: excel vba excel-vba row

我想获取某个范围内单元格的值并将其存储为变量。然后将另一个单元格的值设置为另一个范围作为该变量。然后继续前进到细胞。在捕获了行中的所有单元格之后,我想移动到下一行并使用第一个单元格和第一个变量重新开始。我无法弄清楚如何移动到下一个单元格,然后将新范围中的值设置为变量。这是我到目前为止所得到的

Dim rng As Range
Dim row As Range
Dim cell As Range
Dim card1 As String
Dim card2 As String
Dim card3 As String
Dim card4 As String
Dim card5 As String
Dim valueRng As Range

Set rng = Range("A2:D4")
Set valueRng = ("F2:I4")


For Each row In rng.Rows
 For Each cell In row.Cells
 card1 = cell.Value
 'Set first variable
 'Code here for setting value of for first cell in valueRng

'Move to second cell
'get value
'set variable card2
'Set value of second cell in valueRng

Next cell  
Next row

1 个答案:

答案 0 :(得分:3)

使用行和列计数器代替For each

Dim rng As Range
Dim row As Range
Dim cell As Range
Dim card1 As String
Dim card2 As String
Dim card3 As String
Dim card4 As String
Dim card5 As String
Dim valueRng As Range
Dim x as long
Dim y as long

Set rng = Range("A2:D4")
Set valueRng = Range("F2:I4")


For x = 1 to rng.Rows.Count
   for y = 1 to rng.Columns.Count

 card1 = rng.cells(x, y).Value
 'Set first variable
 'Code here for setting value of for first cell in valueRng
 valueRng.Cells(x, y).Value = card1

Next y  
Next x

请注意,当一个数组具有相同的大小和形状时,可以采用更简单的方法将一个数组填充到另一个数组中。