到达空行时停止VBA宏


0

我最近尝试使用宏来简化Excel 2010下的一些任务,因为我正在使用不幸的巨大数据库。

我已经找到了合并重复行和连接唯一数据/注释所需的代码,这要归功于这个挽救生命的线程:如何将多行中的值组合成Excel中的单行?

对于像我这样的初学者来说代码很容易理解(我确实希望并尝试理解我在做什么而不是盲目地复制粘贴)。我遇到的唯一问题是宏似乎没有停在最后一行,并最终填充excel表的其余部分。

获得了所需的结果,如第4行到第6行所示,但从第29行开始...... 图片 但是你可以看到从第29行开始,宏保持“;” 在第10栏。

这是我改编的代码:

Sub merge_dupes_and_comments()
'define variables

Dim RowNum As Long, LastRow As Long

Application.ScreenUpdating = False

RowNum = 2
LastRow = Cells.SpecialCells(xlCellTypeLastCell).row
Range("A2", Cells(LastRow, 10)).Select

For Each row In Selection
    With Cells
    'if OC number matches
    If Cells(RowNum, 2) = Cells(RowNum + 1, 2) Then
        'and if position and material match
        If Cells(RowNum, 4) = Cells(RowNum + 1, 4) Then
        If Cells(RowNum, 5) = Cells(RowNum + 1, 5) Then
        'move updated comments up next to the old comment and delete empty line
            Cells(RowNum, 10) = Cells(RowNum, 10) & ";" & Cells(RowNum + 1, 10)
            Rows(RowNum + 1).EntireRow.Delete
       End If
        End If
         End If
         End With

RowNum = RowNum + 1
Next row

Application.ScreenUpdating = True

End Sub

我不太清楚为什么它不会停止,我不想输入特定的结束行,因为我正在使用的数据库每周都有所不同。

我试图将最后一行重新定义为:

Dim LastRow As Long

With ThisWorkbook.Worksheets("MasterData") 'enter name of the sheet you're working on
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        LastRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).row
    Else
        LastRow = 1
    End If

但我注意到了任何变化。

我将不胜感激任何帮助!

非常感谢KuroNavi


如果您知道可以退出循环,请考虑exit for跳出循环。此外,您的With Cells语句未在您的代码中使用,您可以考虑删除它。使用单元格,当您输入.statement时,它将使用单元格。是在它之前写的。
LPChip

另外,如果按CTRL-END(我认为这是键盘快捷键)转到最后一个单元格,你最终会在哪里?我感觉底部有空行,导致你的脚本远远超出它需要的范围。当您使用excel打开导出时,这很常见,并且您的数据似乎是导出。或者,创建一个新的宏用于一次性执行,放入最后一行的代码,然后只需执行Cells(LastRow,1).Select并查看它跳转到的位置。是行65535吗?
LPChip

您好,感谢您快速回复并正确格式化我的帖子。我的数据确实是出口。我已经制作了一个干净的新工作表并将表复制到其上。当我尝试输入时, Sub test() Dim LastRow As Long LastRow = Cells.SpecialCells(xlCellTypeLastCell).row Cells(LastRow, 1).Select End Sub 我最终选择了A31,这确实是最后一排。然而,当我重新启动上面提到的代码时,我仍然得到额外的空行和“;”。(对不起,我正在努力与新手格式化...)
KuroNavi

当然会发生这种情况。如果空行匹配空行,则宏会插入;。第29,30和31行是在宏中考虑的3个空行,因为最后一个单元格跳转到31而​​不是29.因此,您要检查行是否为空。我会给你写一个答案。
LPChip

Answers:


0

您的最后一行不是表格的最后一行,但底部包含3个空行,这些行被填充; 因为你的宏包含这一行:

        Cells(RowNum, 10) = Cells(RowNum, 10) & ";" & Cells(RowNum + 1, 10)

这个命令基本上说:用空行连接空行并分开;

但你不想检查空行。所以你的子应该如下:

Sub merge_dupes_and_comments()
    'define variables
    Dim RowNum As Integer, LastRow As Integer, EmptyCells as Integer

    Application.ScreenUpdating = False

    RowNum = 2
    LastRow = Cells.SpecialCells(xlCellTypeLastCell).row
    Range("A2", Cells(LastRow, 10)).Select

    For Each row In Selection
        'Do we have am empty row? If so exit the loop.
        'Lets count the amount of empty cells on the row.
        EmptyCells=0
        For c = 1 to 10   
            if Cells(RowNum,c) = "" then EmptyCells = EmptyCells+1
        Next c

        'If we find more than 9 empty cells, assume the row is empty, and exit the loop.
        if EmptyCells > 9 then exit for

        'Lets continue the duplicate checking
        'if OC number matches
        'and if position and material match
        If Cells(RowNum, 2) = Cells(RowNum + 1, 2) AND _
           Cells(RowNum, 4) = Cells(RowNum + 1, 4) AND _
           Cells(RowNum, 5) = Cells(RowNum + 1, 5) Then
           'move updated comments up next to the old comment and delete empty line
            Cells(RowNum, 10) = Cells(RowNum, 10) & ";" & Cells(RowNum + 1, 10)
            Rows(RowNum + 1).EntireRow.Delete
       End If

    RowNum = RowNum + 1
Next row

Application.ScreenUpdating = True

End Sub

我还将变量的声明从long更改为整数,因为您只使用不超过整数边界的整数,因此消耗的内存更少。


1
非常感谢你!我现在可以看到我哪里出错了 - 你是一个真正的救星,你有我永恒的感激之情!
KuroNavi
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.