循环通过当前行单元格并停止下一个非零值 - 将其保留为活动单元格[关闭]

问题描述 投票:-2回答:1

我有一个包含很长行数据的文件。它们是公式,许多结果都是零值。我想要一个宏将循环通过活动行并停在下一个非零值单元格,将其作为活动单元格。

这将节省滚动浏览非零的时间,并避免在滚动时遗漏它们的人为错误。

我认为(在伪代码中)我想要的东西是:

For active cell To end of row

If value is zero then active cell x+1

Next

End if

End

我不能自己写VBA,只是通过阅读它模糊地理解它在做什么,并且可以复制和修改类似的编码,但我找不到足够接近这个的东西。我不知道如何在顶部声明变量等。

我真的很感激这方面的一些帮助。几个月来我一直在苦苦思索,我认为应该很简单。

vba loops if-statement
1个回答
0
投票

这个问题将因为过于宽泛而被关闭,因为它是。但是,这里有一个简短的片段,我希望将来会帮助你,也许是其他人将来谷歌这个问题:

Public Sub StackOverflowExample()
    Dim LastColumnNumber As Integer
    'get the number of the last column with data in row of the currently selected cell
    'we dont want to go through the entire row when data ends, only until the last cell with data
    LastColumnNumber = ActiveSheet.Rows(ActiveCell.Row).SpecialCells(xlCellTypeLastCell).Column
    'i is our iteration variable, will go from the number of the currently selected cell
    'to the number of the last column with data. we start from the next cell each time
    'otherwise the macro wont work if the selected cell has a non 0 value
    For i = ActiveCell.Column + 1 To LastColumnNumber
        'check if the value of cell with column number i on the current row is not 0
        'Note: if you do a number comparison, empty cells' values will count as 0 as well
        If ActiveSheet.Cells(ActiveCell.Row, i) <> 0 Then
            'select the found cell, scroll the sheet to it and exit the FOR loop
            ActiveSheet.Cells(ActiveCell.Row, i).Select
            Application.Goto ActiveCell, True
            Exit For
        End If
    Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.