为不同的公式选择不同的范围

问题描述 投票:0回答:2

我有一个工作表,需要根据可用的数据计算几个公式,我已经研究了两个这样的公式,但它工作正常但我认为,可能有一个更好的方法。我尝试使用Multirange但无法正确编写语法代码。

Sub CalculateSSL()

Dim lastrow As Integer, val
Dim OutputLastRow As Long
Dim Lstrow
Lstrow = ThisWorkbook.Sheets("All Sheet-Data").Cells(Rows.Count, "A").End(xlUp).Row
Worksheets("All Sheet-Data").Activate

'the below forumla calculates the number of sales with greater than 100000

    Range("L2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("L2:L" & Lstrow)

    Range("W2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("W2:W" & Lstrow)

    Range("AH2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("AH2:AH" & Lstrow)

    Range("AS2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("AS2:AS" & Lstrow)

    Range("BD2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("BD2:BD" & Lstrow)

    Range("BO2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Selection.AutoFill Destination:=Range("BO2:BO" & Lstrow)


'the below forumla calculates the difference between two specific items

    Range("V2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-3]"
    Range("V2").Select
    Selection.AutoFill Destination:=Range("V2:V" & Lstrow)

    Range("AG2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-3]"
    Range("AG2").Select
    Selection.AutoFill Destination:=Range("AG2:AG" & Lstrow)

    Range("AR2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-3]"
    Range("AR2").Select
    Selection.AutoFill Destination:=Range("AR2:AR" & Lstrow)

    Range("BC2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-3]"
    Range("BC2").Select
    Selection.AutoFill Destination:=Range("BC2:BC" & Lstrow)

    Range("BN2").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-3]"
    Range("BN2").Select
    Selection.AutoFill Destination:=Range("BN2:BN" & Lstrow)



End Sub
excel-vba vba excel
2个回答
1
投票

像这样的东西?

Sub CalculateSSL()
    Dim lastrow As Integer, val
    Dim OutputLastRow As Long
    Dim Lstrow
    Dim MySheet As Worksheet, vArr(), i As Long

    Lstrow = ThisWorkbook.Sheets("All TMS-Data").Cells(Rows.Count, "A").End(xlUp).Row
    Set MySheet = ThisWorkbook.Worksheets("All Sheet-Data")

    'the below forumla calculates the number of sales with greater than 100000

    vArr = Array("L", "W", "AH", "AS", "BD", "BO")
    For i = Lbound(vArr) To Ubound(vArr)
        MySheet.Range("" & vArr(i) & "2:" & vArr(i) & Lstrow & "").FormulaR1C1 _
            = "=COUNTIF(RC[1]:RC[8],"">100000"")"
    Next i

    'the below forumla calculates the difference between two specific items

    vArr = Array("V", "AG", "AR", "BC", "BN")
    For i = Lbound(vArr) To Ubound(vArr)
        MySheet.Range("" & vArr(i) & "2:" & vArr(i) & Lstrow & "").FormulaR1C1 _
            = "=RC[-1]-RC[-3]"
    Next i
End Sub

1
投票
Sub CalculateSSL()

Dim lastrow As Integer, val
Dim OutputLastRow As Long
Dim Lstrow
With ThisWorkbook.Sheets("All Sheet-Data")
   Lstrow = .Cells(Rows.Count, "A").End(xlUp).Row
   with .Range("L2:L" & Lstrow)
            .formula ="=COUNTIF(RC[1]:RC[8],"">100000"")"
            .copy destination:=array(.Range("W2"),.Range("AH2"),.Range("AS2"),.Range("BD2"),.Range("BO"))
   End WIth
   With .Range("v2:v" & Lstrow)
        .Formula =  "=RC[-1]-RC[-3]"
        .Copy destination:= array(.Range("BC2"),.Range("AG2"),.Range("AR2"),.range("BN2"))

   End With
End With
End Sub

编辑哎呀 - 我的点引用是错误的.range(“BC2”)意味着扩展到

ThisWorkbook.Sheets("All Sheet-Data").Range("BC2")

但它实际上扩展到了

ThisWorkbook.Sheets(“All Sheet-Data”)。范围(“v2:v”和Lstrow)。范围(“BC2”)

所以我们需要添加一个工作表对象来引用工作表

Dim ws as Worksheet
Set ws = ThisWorkbook.Sheets("All Sheet-Data")

而且它也不是它的联盟

 .copy destination:=union(ws.Range("W2"),ws.Range("AH2"),ws.Range("AS2"),ws.Range("BD2"),ws.Range("BO"))
© www.soinside.com 2019 - 2024. All rights reserved.