Excel VBA - 从另一个工作表中过滤和检索数据的更好方法

时间:2014-11-19 20:37:36

标签: excel vba excel-vba

这可能是一个太宽泛的问题,我道歉,因为我已经看到了关于如何做到这一点的各种参考,但不是全面的,无论如何......

我在过去三周内第一次在Excel中构建宏。我一直在使用记录宏功能,因为我学习了如何硬编码并且事情已经成功运行。也就是说,我知道有更好/更快/更有效的方式去做我正在做的事情。

基本上,我构建宏来使用来自许多其他单独工作簿(由组织的各个部分控制)的数据来更新主仪表板工作簿。我一直在使用的代码看起来像这样 - 而且是宏记录器使用的代码,我模仿了它。

Sub OperatingCostUpdate()

' ' OperatingCostUpdate宏 '从外部数据连接工作簿中提取运营成本信息,并将其移至“运营成本”选项卡以进行月度报告。 '

'Declares community variable

Dim db_community As String

'Pulls community name for use in filter

Windows("Dashboard Main Macro.xlsm").Activate
Sheets("Community Information").Select

If Range("A4") <> "" Then
    db_community = Range("A4")
End If

'Filters sheet based on community

Windows("Billing Attributes With Unit Attributes.xlsx").Activate
ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=1, Criteria1:=Array(2, db_community)

'Declares PM account number variable

Dim db_pmaccount As String

Windows("Dashboard Main Macro.xlsm").Activate
Sheets("Community Information").Select

If Range("C4") <> "" Then
    db_pmaccount = Range("C4")
End If

'Filters based on PM account number

Windows("Billing Attributes With Unit Attributes.xlsx").Activate
ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=2, Criteria1:=Array(2, db_pmaccount)

'Filters based on property type, sets to apartments only

ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=12, Criteria1:="Apartment"

'Delcares date variable

Dim db_date As Date

'Pulls date for selected community for use in filter

Windows("Dashboard Main Macro.xlsm").Activate
Sheets("Community Information").Select

If IsDate(Range("F4")) Then
    db_date = Range("F4")

End If

Windows("Billing Attributes With Unit Attributes.xlsx").Activate
ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=10, Operator:=xlFilterValues, Criteria2:=Array(2, db_date)

'Filters out negative values in calculation columns

ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=6, Criteria1:=">=0", Operator:=xlAnd
ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=7, Criteria1:=">=0", Operator:=xlAnd
ActiveSheet.ListObjects("Table_BillingAttributes_2YRs").Range.AutoFilter Field _
    :=9, Criteria1:=">=0", Operator:=xlAnd

'Pulls data and inserts into report for selected community

Windows("Dashboard Main Macro.xlsm").Activate
Sheets("REPORT").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = _
    "=SUBTOTAL(109, '[Billing Attributes With Unit Attributes.xlsx]BillingAttributes_2YRs'!C7)"
Range("C2").Select
ActiveCell.FormulaR1C1 = _
    "=SUBTOTAL(109, '[Billing Attributes With Unit Attributes.xlsx]BillingAttributes_2YRs'!C6)"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
    "=SUBTOTAL(109, '[Billing Attributes With Unit Attributes.xlsx]BillingAttributes_2YRs'!C9)"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-3]:RC[-1])/'Community Information'!R[2]C"
Range("B2:E2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

是否有更好的方法来过滤和复制此数据并将其移至&#34;仪表板&#34;工作簿?

谢谢!

1 个答案:

答案 0 :(得分:0)

通常说你的代码没有错误,但是你会发现,如果不使用一些变量来引用你常用的项目,你的代码很难维护。

例如:这样的东西更容易修复或修改

Sub TT()
'Declares community variable

    Dim db_community As String
    Dim db_pmaccount As String
    Dim db_date As Date

    Dim shtCI As Worksheet, shtBill As Worksheet, shtReport As Worksheet
    Dim tblBA As ListObject

    Set shtCI = Workbooks("Dashboard Main Macro.xlsm").Sheets("Community Information")


    Set tblBA = Workbooks("Billing Attributes With Unit Attributes.xlsx") _
                .Sheets("sheetName?").ListObjects("Table_BillingAttributes_2YRs")

    'Filters sheet based on community
    db_community = shtCI.Range("A4").Value 'Pulls community name for use in filter
    tblBA.Range.AutoFilter Field:=1, Criteria1:=Array(2, db_community)

    'Filters based on PM account number
    db_pmaccount = shtCI.Range("C4").Value
    tblBA.Range.AutoFilter Field:=2, Criteria1:=Array(2, db_pmaccount)

    'Filters based on property type, sets to apartments only
    tblBA.Range.AutoFilter Field:=12, Criteria1:="Apartment"

    If IsDate(shtCI.Range("F4").Value) Then
        db_date = shtCI.Range("F4").Value
    End If

    tblBA.Range.AutoFilter Field:=10, Operator:=xlFilterValues, _
                             Criteria2:=Array(2, db_date)

    tblBA.Range.AutoFilter Field:=6, Criteria1:=">=0", Operator:=xlAnd
    tblBA.Range.AutoFilter Field:=7, Criteria1:=">=0", Operator:=xlAnd
    tblBA.Range.AutoFilter Field:=9, Criteria1:=">=0", Operator:=xlAnd

    'etc etc

End Sub