如何通过循环使VBA偏移代码更有效

问题描述

我是VBA的新手,我写了一个vba代码,该代码使用Sumif公式,并根据活动单元使用一系列偏移量将其应用于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),ActiveSheet.Columns(7))
    ActiveCell.Offset(0,2).NumberFormat = "_ * #,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),ActiveSheet.Columns(8))
    ActiveCell.Offset(0,3).NumberFormat = "_ * #,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),ActiveSheet.Columns(9))
    ActiveCell.Offset(0,4).NumberFormat = "_ * #,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),ActiveSheet.Columns(10))
    ActiveCell.Offset(0,5).NumberFormat = "_ * #,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),ActiveSheet.Columns(11))
    ActiveCell.Offset(0,6).NumberFormat = "_ * #,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),ActiveSheet.Columns(12))
    ActiveCell.Offset(0,7).NumberFormat = "_ * #,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),ActiveSheet.Columns(13))
    ActiveCell.Offset(0,8).NumberFormat = "_ * #,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