VBA IF然后出现其他循环问题,对比较值和逻辑感到困惑

时间:2019-04-17 05:22:51

标签: excel loops if-statement

我对房子周围的vba项目感到疯狂,并帮助我的妻子将报告提升到一个新的水平。我在想些什么时遇到了麻烦。如果有人感到我的痛苦,请说明一下可以帮助我克服这一困难的实际脚本。摘要可能正在使用InStr比较某些文本的单元格值,如果不存在,则在右端添加字符。我可以在循环的一个周期内追加并运行,但对尝试编写我正在考虑的逻辑感到困惑。

报告的背景很少:一行等于一项保留。在该行中,有一列标记为“夜”。对于超过“ 1”晚的预订,此列将被过滤。例如:可能是3晚,6晚和10晚。我有一个宏,可以对这些保留进行排序,并将一个保留拆分为多行,总计“ Nights”列中的数字值。基本上,将行复制并插入彼此相邻。虽然仍然应用此过滤器(仅适用于SpecialVisibleCells)。现在,我有另一列标记为“ ResNumber”。用3、6或10行拆分出来的“ ResNumber”列是相同的数字。我的任务是向下浏览“ ResNumber”列,并为第一行添加“ -1”。第二个保留的“ -2”为第三个保留的“ -3”,可能的第四个“ -4”直到该保留组的复制的最后一行。然后,循环(循环)在下一组或下一行的行上再次开始。相同的步骤。

Dim Inrow为整数   暗淡作为字符串   昏暗的rownuml作为整数行检查器   Dim colnuml As String'列检查器   昏暗计数为整数   Dim total As String'预订的“ Nights”列的值Offset(,17)   Dim startnum As Integer'计数器的起始号   Dim actcell As String'Activecell   startnum = 1   与sh      llrow = .Cells(.Rows.count,2).End(xlUp).row        如果llrow =“”然后退出Sub          .Cells(2,2).Resize(llrow-1).SpecialCells(xlCellTypeVisible).Select

     For lnrow = 2 To llrow
     rownuml = ActiveCell.row
     colnuml = ActiveCell.Column
     total = ActiveCell.offset(, 17).Value

     For count = 1 To total
     rownuml = ActiveCell.row
     colnuml = ActiveCell.Column
     actcell = ActiveCell.Value

'Compares row 1 and checks resNumber value for "-1" if none exist it appends.
                   If InStr(ActiveCell.Value, "-1") = 0 Then
                        ActiveCell.Value = ActiveCell.Value & "-1"
                     Else
                     GoTo nexrow
                    End If

'Compares row 2 and checks resNumber value of above cell.
           If InStr(ActiveCell.offset(-1, 0).Value, "-1") = 0 Then
                      Resume Next
                    If InStr(ActiveCell.Value, "-2") = 0 Then
                        ActiveCell.Value = ActiveCell.Value & "-2"
                     GoTo nexrow
                    End If

'跳出循环关系     “ ActiveCell向下移动了一行。             ActiveCell.offset(1,0).SpecialCells(xlCellTypeVisible)。选择                rownuml = ActiveCell.row'仅检查行号                colnuml = ActiveCell.Column'仅检查列号

'因为第一个保留已经在数据库startnum中从#1开始。                 startnum = startnum +计数                 下一个计数                 下一个         结尾为enter image description here

1 个答案:

答案 0 :(得分:0)

尝试:

Option Explicit

Sub test()

    Dim LastRow As Long, Times As Long, Counter As Long, i As Long, y As Long
    Dim strNumber As String

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        For i = 2 To LastRow

            strNumber = .Range("B" & i).Value

            Times = Application.WorksheetFunction.CountIf(.Range("B2:B" & LastRow), strNumber)

            If Times > 1 Then

                Counter = 1

                For y = 2 To LastRow

                    If strNumber = .Range("B" & y).Value Then

                        .Range("B" & y).Value = strNumber & " - " & Counter
                        .Range("D" & y).Value = 1
                        Counter = Counter + 1

                    End If

                Next y

            End If

        Next i

    End With

End Sub

结果:

enter image description here