无法使用VBA在不同的工作表上显示相邻单元格的值

时间:2016-09-23 21:28:03

标签: excel vba

这段代码给了我麻烦。我想在B列中扫描一个关键字,并在单独的工作表中显示它旁边的单元格的值(因此它将是A列的同一行)。我编写了代码,并突出了问题部分。它仅显示最终值,而不显示其余值。我将不胜感激任何帮助。

Dim MyRange As Range
Dim rcell As Range
Dim Sum_Payment As Double
Dim Dates_Array() As Double
Dim Cash_Array()  As Double
Dim i As Integer



Set MyRange = Worksheets("Database").Range("B2:B50")

    For Each rcell In MyRange.Cells
            If rcell.Value = "Payment" Then
                ***For i = 1 To 30***
                    Sum_Payment = Sum_Payment + rcell.Offset(0, 2).Value
                    ***Dates_Array(i) = rcell.Offset(i, -1).Value
                    ThisWorkbook.Sheets("Sheet2").Range("A10:A20") = Dates_Array(i)***
                Next i

        End If
    Next rcell
ThisWorkbook.Sheets("Sheet2").Range("B2") = Sum_Payment

1 个答案:

答案 0 :(得分:1)

每次循环时,你都会覆盖相同的范围。如果我理解你的描述,这一行......

ThisWorkbook.Sheets("Sheet2").Range("A10:A20") = Dates_Array(i)

......应该是:

ThisWorkbook.Sheets("Sheet2").Cells(rcell.Row, 1) = Dates_Array(i)