如何在vba中设置此动态范围?

时间:2016-08-08 02:05:57

标签: excel vba excel-vba

这是3个工作表。

button

report

day

案例:

我想总结每个人的日子。结果显示在工作表报告中。

日期信息存储在工作表日中。这一天不固定。日期的范围是动态的,应根据

row: that person row in worksheet-day
column :Worksheets("button").Cells(1, 2 ) to Worksheets("button").Cells(2, 2)

例如:

Worksheets("button").Cells(1, 2 ) 'store 3

Worksheets("button").Cells(2, 2 )  'store 5

在工作表报告的A栏中,应计算Peter,Tom,Mary的总和。结果应相应地显示在B栏上。

然后,应在工作表日搜索它们的总和,并将该总数加在范围内(D:F',因为工作表中的值(“按钮”))

结果应为:

Peter: 2
Tom:   3
Mary:  3

最后,此结果相应地显示在工作表 - 报告列B上。

这是我的代码:

Sub gg()
row = Worksheets("report").Range("A" & Rows.Count).End(xlUp).row
Row2 = Worksheets("day").Range("A" & Rows.Count).End(xlUp).row
Dim rng As Range  'the range for sum
Dim row999 As Integer 'store the selected row index

For k = 2 To Row2 'check and store appropriate row
    If Worksheets("report").Cells(1, k).Value = Worksheets("day").Cells(1, k).Value Then
        row999 = ActiveCell.row
    End If
Next k

rng = Range(Cells(Worksheets("button").Cells(1, 2), row999), Cells(Worksheets("button").Cells(2, 2), row999))

For j = 2 To row  'do the sum
    Worksheets("report").Cells(2, i) = Application.WorksheetFunction.Sum(rng)
Next j
End Sub

我猜这里的rng不准确。

rng = Range(Cells(Worksheets("button").Cells(1, 2), row999), Cells(Worksheets("button").Cells(2, 2), row999))

2 个答案:

答案 0 :(得分:0)

传递给Cells的第一个参数是行,第二个参数是列。

您还应该确定范围所在的工作表。

所以你的话说:

rng = Range(Cells(Worksheets("button").Cells(1, 2), row999), Cells(Worksheets("button").Cells(2, 2), row999))

应该说

rng = Worksheets("day").Range(Cells(row999, Worksheets("button").Cells(1, 2)), _
                              Cells(row999, Worksheets("button").Cells(2, 2)))

但是,您的代码中还有很多其他错误。我相信你想要的是:

Sub gg()
    Dim lastrowReport As Long
    Dim lastrowDay As Long
    Dim rowReport As Long
    Dim rowDay As Long
    lastrowReport = Worksheets("report").Range("A" & Rows.Count).End(xlUp).row
    lastrowDay = Worksheets("day").Range("A" & Rows.Count).End(xlUp).row
    Dim rng As Range  'the range for sum

    For rowReport = 2 To lastrowReport ' loop through each of the rows on the report
        For rowDay = 2 To lastrowDay   ' find the entry for this person on the days sheet
            If Worksheets("report").Cells(rowReport, 1).Value = Worksheets("day").Cells(rowDay, 1).Value Then
                set rng = Worksheets("day").Range(Worksheets("day").Cells(rowDay, Worksheets("button").Cells(1, 2)), _
                                                  Worksheets("day").Cells(rowDay, Worksheets("button").Cells(2, 2)))
                Worksheets("report").Cells(rowReport, 2) = Application.WorksheetFunction.Sum(rng)
                Exit For
            End If
        Next
    Next 

End Sub

答案 1 :(得分:0)

您遇到的问题是名称不匹配。汤姆在报告上是大写,在当天是小写。玛丽在Day工作表上有一个额外的空间" mary"。

尽可能使用较短的变量名称,这样您就可以阅读每行代码而无需用眼睛来回扫描。

Sub ProcessDays()
    Dim day1 As Integer, day2 As Integer
    Dim i As Integer, j As Integer
    Dim name1 As String, name2 As String

    With Worksheets("button")
        day1 = .Cells(1, 2)
        day2 = .Cells(2, 2)
    End With

    With Worksheets("day")

        For i = 2 To Worksheets("report").Range("A" & Rows.Count).End(xlUp).Row
            For j = 2 To .Range("A" & Rows.Count).End(xlUp).Row
                name1 = UCase(Trim(Worksheets("report").Cells(i, 1)))
                name2 = UCase(Trim(.Cells(j, 1)))

                If name1 = name2 Then

                    Set rng = .Range(.Cells(j, day1), .Cells(j, day2))

                    Worksheets("report").Cells(i, 2) = WorksheetFunction.Sum(rng)

                End If
            Next
        Next

    End With

End Sub