在VBA中添加新行故障

在VBA中添加新行故障,vba,excel,Vba,Excel,我已经编写了以下代码,在电子表格中的一个部分内的一系列行中添加了新行。由于有多个节,sBudgetLine作为范围(节名称)的起始值传入,该范围加上额外的1以忽略节的标题(日期、说明和价格) 所有的工作都很好,但是,当偶尔添加行时,我硬编码的格式不起作用,并在页面下方添加了一行额外的行以及位于正确位置的新行 Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = c_Alloc2Spend

我已经编写了以下代码,在电子表格中的一个部分内的一系列行中添加了新行。由于有多个节,sBudgetLine作为范围(节名称)的起始值传入,该范围加上额外的1以忽略节的标题(日期、说明和价格)

所有的工作都很好,但是,当偶尔添加行时,我硬编码的格式不起作用,并在页面下方添加了一行额外的行以及位于正确位置的新行

Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = c_Alloc2SpendSheetName)

Dim c As Range
Dim N As Long
Dim lastRow As Long
Dim formula As Range
Dim rng As Range

With Worksheets(sSheetName)

    Set c = .Columns(1).Find(sBudgetLine, LookIn:=xlValues)
    If Not (c Is Nothing) Then

        lastRow = .Cells(c.Row, 2).End(xlDown).Row

        Worksheets(sSheetName).Activate
        Worksheets(sSheetName).Range("B" & lastRow + 1, "E" & lastRow + 1).Activate

        Selection.EntireRow.Insert Shift:=xlDown

        Selection.Range("B" & ActiveCell.Row, "E" & ActiveCell.Row).Interior.Color = RGB(255, 255, 255)

        With Selection.Range("B" & ActiveCell.Row, "D" & ActiveCell.Row).Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = 1
            End With

        Selection.Cells(1, 4).formula = "=IF(" & "D" & ActiveCell.Row & "="""","""",IF(" & "D" & ActiveCell.Row & ">14999.99,""Minimum 3 formal competitive tenders - inform PSU"",IF(" & "D" & ActiveCell.Row & ">2499.99,""Minimum 3 written quotes based on written specification"",IF(" & "D" & ActiveCell.Row & ">999.99,""Minimum 3 written quotes"",IF(" & "D" & ActiveCell.Row & ">499.99,""Minimum 3 oral quotes"",IF(" & "D" & ActiveCell.Row & ">249.99,""One written or verbal quote"",""One written or verbal quote""))))))"

    End If
End With
End Sub
我尝试了各种各样的方法,但都不知道出了什么问题,这可能是vba故障吗?如果是这种情况,是否有更好的方法为添加的行提供格式?

您从引用父工作表开始,然后似乎放弃了它。如果包含所需单元的较大单元块已为选定单元,则选择不会更改,仅更改

最好避免使用方法和方法

Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = c_Alloc2SpendSheetName)

    Dim rw As Variant, nr As Long

    With Worksheets(sSheetName)
        rw = Application.Match(sBudgetLine, .Columns(1), 0)
        If Not IsError(rw) Then
            nr = .Cells(rw, "B").End(xlDown).Row + 1
            .Rows(nr).EntireRow.Insert Shift:=xlDown
            With .Range(.Cells(nr, "B"), .Cells(nr, "E"))
                .Interior.Color = RGB(255, 255, 255)
                With .Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = 1
                End With
                .Cells(1, 4).formula = _
                    "=IF(D" & nr & "=TEXT(,), TEXT(,), " & _
                     "IF(D" & nr & ">14999.99, ""Minimum 3 formal competitive tenders - inform PSU"", " & _
                     "IF(D" & nr & ">2499.99, ""Minimum 3 written quotes based on written specification"", " & _
                     "IF(D" & nr & ">999.99, ""Minimum 3 written quotes"", " & _
                     "IF(D" & nr & ">499.99, ""Minimum 3 oral quotes"", " & _
                     "IF(D" & nr & ">249.99, ""One written or verbal quote"", " & _
                        """One written or verbal quote""))))))"
            End With
        End If

    End With
End Sub
文本(,)
只是一种写入
的方法,而不必将其作为字符串写入



.1有关如何摆脱依靠选择和激活来实现目标的更多方法,请参阅。

这太棒了,谢谢。我是来自C#的VBA新手,因此它是理解语法的雷区!