如何在一个范围内的单个单元格中输入值,并继续遍历该范围内的每个单元格

时间:2019-01-22 16:13:29

标签: excel vba loops

我的范围A6:A24为空白。我想将值1粘贴到每个单元格中,然后将H9中的计算结果复制到新工作表中。之后,我要移至下一个单元格,粘贴“ 1”,但删除前一个“ 1”,然后粘贴结果值。

我可以将1粘贴到每个框中,也可以只粘贴到顶部。

A6:A24是年。我试图将每年等于1(100%)的时候的计算取值,这意味着所有其他年份都需要等于零。

Private Sub CommandButton1_Click()

Dim inputRange1 As Range
Dim inputRange2 As Range
Dim c As Range
Dim i As Long
Dim b As Range
Dim j As Long

Set dvCell2 = Worksheets("Sheet1").Range("A6:A24")

Set inputRange2 = Worksheets("Sheet1").Range("D1")

Set dvCell1 = Worksheets("Sheet2").Range("C1")

Set inputRange1 = Worksheets("Sheet1").Range("B6:B24")

i = 1
j = 1

Application.ScreenUpdating = False
For Each b In inputRange2
    dvCell2.Value = b.Value
    For Each c In inputRange1
        dvCell1.Value = c.Value
        Worksheets("Sheet4").Cells(i + 2, j + 3).Value = Worksheets("Sheet3").Range("H9").Value
        i = i + 1
    Next c
    j = j + 1   
    i = 1
Next b
Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

不确定我是否遵循。这将循环遍历dvcell2中的每个单元格,并在其中放入1,然后复制H9的值。我不确定您是否要尝试做其他事情。

Private Sub CommandButton1_Click()

Dim inputRange1 As Range
Dim inputRange2 As Range
Dim c As Range
Dim i As Long
Dim b As Range
Dim j As Long

Set dvcell2 = Worksheets("Sheet1").Range("A6:A24")
Set inputRange2 = Worksheets("Sheet1").Range("D1")
Set dvCell1 = Worksheets("Sheet2").Range("C1")
Set inputRange1 = Worksheets("Sheet1").Range("B6:B24")

i = 1
j = 1

Application.ScreenUpdating = False

For Each b In dvcell2
    dvcell2.value=0
    b.Value = 1
    Worksheets("Sheet4").Cells(i + 2, j + 3).Value = Worksheets("Sheet3").Range("H9").Value
    j = j + 1
Next b

Application.ScreenUpdating = True

End Sub