如何使用循环使我的 VBA 偏移代码更高效

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

我是 VBA 的新手,我写了一个 vba 代码,它使用 Sumif 公式并使用基于活动单元格的一系列偏移量将其应用到 F 列到 N 列.我知道有一种更有效的方法可以做到这一点,并希望能指出正确的方向。我正在对更多变量执行此求和公式,以便能够利用结果。

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 放在模块的顶部,子模块之外。更好的是去 Tools --> Options --> Editor --> Require Variable Declaration,它会一直在那里。

我将循环硬编码为 i = 1 to 8 如果那是最后一列,您也可以找到最后一列。

通过索引引用表格通常不是一个好主意,它很容易改变。

我删除了你的 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