Excel VBA如果条件满足则插入新行,并在条件满足时填充颜色

Jyotirmaya Chandra
Option Explicit

Sub InsertRowBelowNegativeEntriesInFGHI()

    Dim lLastColRow As Long
    Dim lLastRow As Long
    Dim lColIndex As Long
    Dim lRowIndex As Long
    Dim bInsert As Boolean
    Dim bIsBalanceRow As Boolean
    Dim vFPos As Variant
    Dim vGPos As Variant
    Dim vHPos As Variant
    Dim vIPos As Variant
    Dim vJPos As Variant
    Dim vKPos As Variant
    Dim vLPos As Variant
    Dim vMPos As Variant
    Dim vNPos As Variant
    Dim vOPos As Variant
    Dim vPPos As Variant
    Dim vQPos As Variant
    Dim vRPos As Variant
    Dim vSPos As Variant
    Dim vTPos As Variant
    Dim sTrigger As String

    For lColIndex = 6 To 10
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next

    For lRowIndex = lLastRow - 1 To 2 Step -1
        If UCase(Cells(lRowIndex, 1).Value) = "BALANCE" Then
            'On a BALANCE row
            bInsert = False
            vFPos = Cells(lRowIndex, "F").Value
            vGPos = Cells(lRowIndex, "G").Value
            vHPos = Cells(lRowIndex, "H").Value
            vIPos = Cells(lRowIndex, "I").Value
            vJPos = Cells(lRowIndex, "J").Value

            If vFPos < 0 And (vGPos > 0 Or vHPos > 0 Or vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "F"
            If vGPos < 0 And (vHPos > 0 Or vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "G"
            If vHPos < 0 And (vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "H"
            If vIPos < 0 And (vJPos > 0) Then bInsert = True: 'sTrigger = "I"

            If bInsert Then
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
                'Debug.Print lRowIndex, sTrigger
            End If

        End If
    Next
    For lColIndex = 11 To 15
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next

    For lRowIndex = lLastRow - 1 To 2 Step -1
        If UCase(Cells(lRowIndex, 1).Value) = "BALANCE" Then
            'On a BALANCE row
            bInsert = False
            vKPos = Cells(lRowIndex, "K").Value
            vLPos = Cells(lRowIndex, "L").Value
            vMPos = Cells(lRowIndex, "M").Value
            vNPos = Cells(lRowIndex, "N").Value
            vOPos = Cells(lRowIndex, "O").Value

            If vKPos < 0 And (vLPos > 0 Or vMPos > 0 Or vNPos > 0 Or vOPos > 0) Then bInsert = True: 'sTrigger = "K"
            If vLPos < 0 And (vMPos > 0 Or vNPos > 0 Or vOPos > 0) Then bInsert = True: 'sTrigger = "L"
            If vMPos < 0 And (vNPos > 0 Or vOPos > 0) Then bInsert = True: 'sTrigger = "M"
            If vNPos < 0 And (vOPos > 0) Then bInsert = True: 'sTrigger = "N"

            If bInsert Then
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
                'Debug.Print lRowIndex, sTrigger
            End If

        End If
    Next
    For lColIndex = 16 To 20
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next

    For lRowIndex = lLastRow - 1 To 2 Step -1
        If UCase(Cells(lRowIndex, 1).Value) = "BALANCE" Then
            'On a BALANCE row
            bInsert = False
            vPPos = Cells(lRowIndex, "P").Value
            vQPos = Cells(lRowIndex, "Q").Value
            vRPos = Cells(lRowIndex, "R").Value
            vSPos = Cells(lRowIndex, "S").Value
            vTPos = Cells(lRowIndex, "T").Value

            If vPPos < 0 And (vQPos > 0 Or vRPos > 0 Or vSPos > 0 Or vTPos > 0) Then bInsert = True: 'sTrigger = "P"
            If vQPos < 0 And (vRPos > 0 Or vSPos > 0 Or vTPos > 0) Then bInsert = True: 'sTrigger = "Q"
            If vRPos < 0 And (vSPos > 0 Or vTPos > 0) Then bInsert = True: 'sTrigger = "R"
            If vSPos < 0 And (vTPos > 0) Then bInsert = True: 'sTrigger = "S"

            If bInsert Then
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
                'Debug.Print lRowIndex, sTrigger
            End If

        End If
    Next
End Sub

我正在使用上面的代码来查找在余额行的FGHIJ,KLMNO,PQRST列中是否有任何负数后跟正数。在A列中有多个“余额”行。

上面的代码正常工作,并且在从左到右依次为负值和负值的情况下,在余额上方插入新行。但是,对于PQRST列(对于16-20列)而言,它不起作用,我不知道为什么,并且对此代码需要进行哪些更改?

  1. 如果条件满足,我想添加2行,而不是全部3个分类列(FGHIJ)(KLMNO)(PQRST)中的1行
  2. 我想在第一个空白添加行的A列中有一个单词“ By Adjustment”。
  3. 我希望该部分应填充为符合条件的绿色。

例如在F6 G6 H6 I6 J6中的值为0 -10 100 0 10

此处将添加2个新行,然后用绿色填充F6 G6 H6 I6 J6。

满足条件的任何地方都应该用绿色上色,并插入两行空白。

用户名

当子例程与您的例程一样复杂时,应简化将任务委派给其他子例程和函数的过程。

Sub InsertRowBelowNegativeEntriesInFGHI2()
    Dim lLastRow As Long, lRowIndex As Long
    Dim InsertF As Boolean, InsertK As Boolean, InsertP As Boolean

    lLastRow = Range(Columns(6), Columns(20)).Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

    For lRowIndex = lLastRow To 2 Step -1
        If UCase(Cells(lRowIndex, 1).Value) = "BALANCE" Then
            InsertF = ShouldInsert(lRowIndex, "F")
            InsertK = ShouldInsert(lRowIndex, "K")
            InsertP = ShouldInsert(lRowIndex, "P")

            If InsertF And InsertK And InsertP Then
                Rows(lRowIndex & ":" & lRowIndex + 1).Insert , CopyOrigin:=xlFormatFromLeftOrAbove

                Range(Cells(lRowIndex, "F"), Cells(lRowIndex + 1, "T")).Interior.Color = vbGreen
                Cells(lRowIndex, 1) = "By Adjustment"
                Cells(lRowIndex, 1).Offset(1) = "By Adjustment"
            ElseIf InsertF Or InsertK Or InsertP Then
                Rows(lRowIndex).Insert , CopyOrigin:=xlFormatFromLeftOrAbove

                If InsertF Then Range(Cells(lRowIndex, "F"), Cells(lRowIndex, "J")).Interior.Color = vbGreen
                If InsertK Then Range(Cells(lRowIndex, "K"), Cells(lRowIndex, "O")).Interior.Color = vbGreen
                If InsertP Then Range(Cells(lRowIndex, "P"), Cells(lRowIndex, "T")).Interior.Color = vbGreen

                Cells(lRowIndex, 1) = "By Adjustment"
            End If
        End If

    Next

End Sub

Function ShouldInsert(xRow As Long, firstColumnLetter As String) As Boolean
    Dim y As Integer
    Dim bNegative
    Dim c As Range
    Set c = Cells(xRow, firstColumnLetter)
    Dim a(4) As Double

    For y = 0 To 3
        If c.Offset(0, y) < 0 Then bNegative = True

        If bNegative And c.Offset(0, y + 1) > 0 Then
            ShouldInsert = True
            Exit Function
        End If

    Next

End Function

Function OldShouldInsert1(xRow As Long, firstColumnLetter As String) As Boolean
    Dim c As Range
    Set c = Cells(xRow, firstColumnLetter)

    ShouldInsert = (c.Offset(0, 0).Value < 0 And (c.Offset(0, 1) > 0 Or c.Offset(0, 2) > 0 Or c.Offset(0, 3) > 0 Or c.Offset(0, 4) > 0)) _
    Or (c.Offset(0, 2).Value < 0 And (c.Offset(0, 3) > 0 Or c.Offset(0, 4))) _
    Or (c.Offset(0, 3).Value < 0 And (c.Offset(0, 4) > 0 Or c.Offset(0, 5) > 0)) _
    Or (c.Offset(0, 4).Value < 0 And (c.Offset(0, 4) > 0))

End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel VBA如果条件满足则插入新行,并在条件满足时填充颜色

来自分类Dev

Excel VBA在满足条件时添加新行

来自分类Dev

满足条件时重置循环excel vba

来自分类Dev

Excel VBA:如果满足条件,则从处理中跳过整个行

来自分类Dev

Excel:如果满足条件,则对列和行求和

来自分类Dev

Excel-条件满足时将数据转换为行

来自分类Dev

满足条件的Excel VBA停止代码

来自分类Dev

Excel VBA尝试将范围设置为列并在满足条件时调用数据

来自分类Dev

如果满足某些条件,请选中复选框Excel VBA

来自分类Dev

如果使用 Excel VBA 不满足条件,则编辑特定列中单元格的值

来自分类Dev

Excel:如果值是数字并满足相对条件,则求和

来自分类Dev

Excel公式求和,如果其他列满足某些条件

来自分类Dev

Excel公式求和,如果其他列满足某些条件

来自分类Dev

Excel-如果满足表B中的条件,如何对表A中的行求和

来自分类Dev

Excel VBA:如何使UDF在不满足条件时返回0

来自分类Dev

满足条件时如何在Excel中删除整行

来自分类Dev

打开Excel文件并满足条件时自动显示MsgBox

来自分类Dev

在创建新的excel工作簿时以及在满足条件时创建新的工作表时的运行总计

来自分类Dev

excel vba-如果满足条件,则将具有各种形状的特定行复制/粘贴到另一张工作表

来自分类Dev

Excel VBA-如果单元格值满足条件,则在工作表之间复制单元格

来自分类Dev

Excel宏——返回满足条件的项目列表

来自分类Dev

Excel SUMIF-如果满足条件,则将同一行中的两个单元格相加

来自分类Dev

需要帮助在单个单元格中获取多个值并在Excel中满足条件

来自分类Dev

Excel,如果满足条件,则对包含字母数字单元格的单元格进行计数

来自分类Dev

如果满足特定条件,则在Excel中确定两个日期之间的天数

来自分类Dev

如果满足特定条件,则在Excel中确定两个日期之间的天数

来自分类Dev

如果还满足第二个条件,Excel会计算唯一值

来自分类Dev

如果满足某些条件,则标识列表中条目的首次出现(EXCEL)

来自分类Dev

Excel如果条件满足,则在多列中查找并比较值

Related 相关文章

  1. 1

    Excel VBA如果条件满足则插入新行,并在条件满足时填充颜色

  2. 2

    Excel VBA在满足条件时添加新行

  3. 3

    满足条件时重置循环excel vba

  4. 4

    Excel VBA:如果满足条件,则从处理中跳过整个行

  5. 5

    Excel:如果满足条件,则对列和行求和

  6. 6

    Excel-条件满足时将数据转换为行

  7. 7

    满足条件的Excel VBA停止代码

  8. 8

    Excel VBA尝试将范围设置为列并在满足条件时调用数据

  9. 9

    如果满足某些条件,请选中复选框Excel VBA

  10. 10

    如果使用 Excel VBA 不满足条件,则编辑特定列中单元格的值

  11. 11

    Excel:如果值是数字并满足相对条件,则求和

  12. 12

    Excel公式求和,如果其他列满足某些条件

  13. 13

    Excel公式求和,如果其他列满足某些条件

  14. 14

    Excel-如果满足表B中的条件,如何对表A中的行求和

  15. 15

    Excel VBA:如何使UDF在不满足条件时返回0

  16. 16

    满足条件时如何在Excel中删除整行

  17. 17

    打开Excel文件并满足条件时自动显示MsgBox

  18. 18

    在创建新的excel工作簿时以及在满足条件时创建新的工作表时的运行总计

  19. 19

    excel vba-如果满足条件,则将具有各种形状的特定行复制/粘贴到另一张工作表

  20. 20

    Excel VBA-如果单元格值满足条件,则在工作表之间复制单元格

  21. 21

    Excel宏——返回满足条件的项目列表

  22. 22

    Excel SUMIF-如果满足条件,则将同一行中的两个单元格相加

  23. 23

    需要帮助在单个单元格中获取多个值并在Excel中满足条件

  24. 24

    Excel,如果满足条件,则对包含字母数字单元格的单元格进行计数

  25. 25

    如果满足特定条件,则在Excel中确定两个日期之间的天数

  26. 26

    如果满足特定条件,则在Excel中确定两个日期之间的天数

  27. 27

    如果还满足第二个条件,Excel会计算唯一值

  28. 28

    如果满足某些条件,则标识列表中条目的首次出现(EXCEL)

  29. 29

    Excel如果条件满足,则在多列中查找并比较值

热门标签

归档