簡體   English   中英

如何使用循環使我的 VBA 偏移代碼更高效

[英]How can I make my VBA offset code more efficient with loop

我是 VBA 的新手,我寫了一個使用 Sumif 公式的 vba 代碼,並使用一系列基於活動單元的偏移量將其應用於 F 到 N 列。 我知道有一種更有效的方法可以做到這一點,並希望能指出正確的方向。 我正在對更多變量執行此 sumif 公式,以便能夠利用結果。

Set ws = Sheets(1)

ws.Cells(ws.Rows.Count, 5).End(xlUp).Offset(2, 0).Select
    ActiveCell.Value = "Total Other Non-U.S. Insurers"
    With ActiveCell.Font
    .Bold = True
    .Size = 9
    End With
    ActiveCell.HorizontalAlignment = xlRight
    
    ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(6))
    ActiveCell.Offset(0, 1).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 1).Font.Size = 9
    With ActiveCell.Offset(0, 1).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

    ActiveCell.Offset(0, 2).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(7))
    ActiveCell.Offset(0, 2).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 2).Font.Size = 9
    With ActiveCell.Offset(0, 2).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With


    ActiveCell.Offset(0, 3).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(8))
    ActiveCell.Offset(0, 3).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 3).Font.Size = 9
    With ActiveCell.Offset(0, 3).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

    ActiveCell.Offset(0, 4).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(9))
    ActiveCell.Offset(0, 4).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 4).Font.Size = 9
    With ActiveCell.Offset(0, 4).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

    ActiveCell.Offset(0, 5).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(10))
    ActiveCell.Offset(0, 5).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 5).Font.Size = 9
    With ActiveCell.Offset(0, 5).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

    ActiveCell.Offset(0, 6).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(11))
    ActiveCell.Offset(0, 6).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 6).Font.Size = 9
    With ActiveCell.Offset(0, 6).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With


    ActiveCell.Offset(0, 7).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(12))
    ActiveCell.Offset(0, 7).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 7).Font.Size = 9
    With ActiveCell.Offset(0, 7).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

    ActiveCell.Offset(0, 8).Value = Application.WorksheetFunction.SumIf(ActiveSheet.Columns(5), "Other Non-U.S. Insurers", ActiveSheet.Columns(13))
    ActiveCell.Offset(0, 8).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
    ActiveCell.Offset(0, 8).Font.Size = 9
    With ActiveCell.Offset(0, 8).Borders(xlEdgeBottom)
        .LineStyle = xlDouble
        .Weight = xlThin
    End With

Option Explicit放在模塊的頂部,在 sub 之外。 更好的是轉到Tools --> Options --> Editor --> Require Variable Declaration ,它會一直存在。

我將循環硬編碼為i = 1 to 8 ,如果這是最后一列,您也可以找到最后一列。

通過索引引用 Sheets 通常不是一個好主意,它很容易改變。

我刪除了您的SelectsActiveCell引用,它們不是必需的。

    Dim ws As Worksheet 'Declare your variables
    
    Set ws = Sheets("Sheet1") 'Change to whatever your sheet name is
    
    With ws.Cells(ws.Rows.Count, 5).End(xlUp).Offset(2, 0) 'No need to Select
        .Value = "Total Other Non-U.S. Insurers"
        With .Font
            .Bold = True
            .Size = 9
        End With
        .HorizontalAlignment = xlRight
        
        Dim i As Long
        For i = 1 To 8
            'You already have ws set no need to use activesheet
            .Offset(0, i).Value = Application.WorksheetFunction.SumIf(ws.Columns(5), "Other Non-U.S. Insurers", ws.Columns(i + 5))
            .Offset(0, i).NumberFormat = "_ * #,##0_)_ ;_ * (#,##0)_ ;_ * "" - ""??_)_ ;_ @_ "
            .Offset(0, i).Font.Size = 9
            With .Offset(0, i).Borders(xlEdgeBottom)
                .LineStyle = xlDouble
                .Weight = xlThin
            End With
        Next i
    End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM