设置动态范围vba

时间:2016-07-17 17:51:54

标签: excel excel-vba macros vba

我一直在网上搜索我的范围在以后提到时不起作用的原因,但仍然是一种损失。非常感谢任何帮助!

我尝试做的是根据可能移动的最后一行设置一个范围(我导入数据,每周增长)。但是,当我到达Set ExpenseNameRange时,出于某种原因它并不认为它是一个范围。

我需要将其用于sumproduct公式中的范围。

以下是我的代码。非常感谢任何帮助!

Dim Profitability As Worksheet
Dim Time As Worksheet
Dim Expense As Worksheet
Dim ExpenseValueRange As Range
Dim ExpenseNameRange As Range
Dim ExpenseDateRange As Range
Dim e As Integer
Dim d As Integer


Set Carryover = ThisWorkbook.Worksheets("2016 Carryover Forecast")
Set Profitability = ThisWorkbook.Worksheets("Profitability")
Set Time = ThisWorkbook.Worksheets("SYNC Time")
Set Expense = ThisWorkbook.Worksheets("SYNC Expense")


finalrowexpense = Expense.Range("A100000").End(xlUp).Row
**Set ExpenseNameRange = Expense.Range(Cells(2, 12), Cells(finalrowexpense, 12))**
Set ExpenseDateRange = Expense.Range(Cells(2, 19), Cells(finalrowexpense, 19))
Set ExpenseValueRange = Expense.Range(Cells(2, 23), Cells(finalrowexpense, 23))
For e = 37 To 63

            employeename = Carryover.Cells(e, 33).Value

            For d = 34 To 41

            If employeename <> "" Then

            ExpenseSum = Application.WorksheetFunction.SumProduct(Month(ExpenseDateRange) = Month(Cells(35, d)), ExpenseNameRange = employeename)

            ExpenseSum = employeename.Offset(0, d).Value

2 个答案:

答案 0 :(得分:2)

考虑:

With Expense
  Set ExpenseNameRange = Range(.Cells(2, 12), .Cells(finalrowexpense, 12))
  Set ExpenseDateRange = Range(.Cells(2, 19), .Cells(finalrowexpense, 19))
  Set ExpenseValueRange =Range(.Cells(2, 23), .Cells(finalrowexpense, 23))
End With

因为Cells()本身就是指ActiveSheet。

修改#1:

此外,您必须使用 SUMPRODUCT()修复Cells()行。

修改#2:

如果Cells()符合条件,则Range()不需要:

Sub dural()
    Dim r As Range

    Sheets("Sheet1").Activate
    Sheets("Sheet1").Select

    With Sheets("Sheet2")
        Set r = Range(.Cells(1, 1), .Cells(2, 2))
        MsgBox r.Parent.Name
    End With
End Sub

答案 1 :(得分:0)

感谢您的精彩答案!我设法远远地跟着兔子洞,想出了其他有用的东西。根据我所做的阅读,看起来Sumifs对于sumproduct来说是一个更好的选择。 Sumifs不会要求使用数组或任何其他特殊因素。

如果有人有兴趣,这是我的循环代码。

Sub Organize_Expenses()

Dim Carryover As Worksheet
Dim Profitability As Worksheet
Dim Time As Worksheet
Dim Expense As Worksheet
Dim ExpenseValueRange As Range
Dim ExpenseNameRange As Range
Dim ExpenseMonthRange As Range
Dim ExpenseYearRange As Range
Dim VendorNameRange As Range
Dim e As Integer
Dim d As Integer
Dim a As Integer
Dim b As Integer


Set Carryover = ThisWorkbook.Worksheets("2016 Carryover Forecast")
Set Profitability = ThisWorkbook.Worksheets("Profitability")
Set Time = ThisWorkbook.Worksheets("SYNC Time")
Set Expense = ThisWorkbook.Worksheets("SYNC Expense")
finalrowexpense = Expense.Range("A100000").End(xlUp).Row

Set ExpenseNameRange = Expense.Range("L2:L" & finalrowexpense)
Set ExpenseMonthRange = Expense.Range("AC2:AC" & finalrowexpense)
Set ExpenseValueRange = Expense.Range("w2:w" & finalrowexpense)
Set ExpenseYearRange = Expense.Range("AD2:AD" & finalrowexpense)


            For d = 1 To 8
            For e = 37 To 56

            Employeename = Carryover.Cells(e, 33).Value

            If Employeename <> "" Then

            ExpenseSum = Application.WorksheetFunction.SumIfs(ExpenseValueRange, ExpenseNameRange, Employeename, ExpenseMonthRange, d, ExpenseYearRange, "2016")



Carryover.Cells(e, 33).Offset(0, d).Value = ExpenseSum

            End If

            Next e

            Next d

 End Sub