我已经写了几行代码,它们也可以像我想要的那样工作,但是我不知道如何在数据的所有行中重复它。 这看起来似乎很简单,但是自从几天前我启动VBA以来,我在这行代码中苦苦挣扎
如果我在代码之后继续ActiveCell.Offset(-1,-4)
,那是一个错误,并且我不知道如何在所有行中重复该代码。
任何帮助将不胜感激。
Sub SelectRowsWithNoBlanks()
Range("A2").Select
If ActiveCell.Offset(0, 0).Value <> "" And ActiveCell.Offset(0, 1) <> "" And ActiveCell(0, 1) <> "" And ActiveCell(0, 1) <> "" Then
Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 4)).Select
End If
End Sub
答案 0 :(得分:1)
@SiddharthRout我没有访问数据的权限,但我无法确定。但我认为以后再将代码扩展到更多列将不是问题。因此,在我现在编写的代码中,我正在检查A-D列,但我认为我可以根据需要轻松添加“检查”以获取更多列– 43年前的安娜·冯·布洛恩
在这种情况下,这是示例代码。
逻辑
.Select
。处理对象。Application.WorksheetFunction.CountA
填充的单元格的数量。因此,如果是列A
至D
,则应填充4个单元格以将“行”视为已填充。同样,对于A到E列,应该填充5个单元格,以将“行”视为已填充,依此类推。代码
我已注释了代码。因此,如果您在理解它时遇到问题,请告诉我。
Option Explicit
Sub SelectRowsWithNoBlanks()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Dim myRange As Range, rng As Range
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Find the last row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Loop through the rows
For i = 2 To lRow
'~~> Change this as applicable
Set rng = .Range("A" & i & ":D" & i)
'~~> Check if the range is completely filled
If Application.WorksheetFunction.CountA(rng) = rng.Columns.Count Then
'~~> Store the range in a range object
If myRange Is Nothing Then
Set myRange = rng
Else
Set myRange = Union(myRange, rng)
End If
End If
Next i
End With
'If Not myRange Is Nothing Then Debug.Print myRange.Address
'~~> Check if any filled rows were found
If Not myRange Is Nothing Then
With myRange
'
'~~> Do what you want with the range
'
End With
Else
MsgBox "No filled rows were found"
End If
End Sub