Excel VBA循环范围并跳过空白

时间:2017-09-24 00:57:39

标签: excel vba excel-vba

我有以下vba代码在单元格L26及以下循环通过范围,它是否正常工作但我想跳过如果单元格是空白的,我用Google搜索并找到此代码:

For i = 10 To 49 
    Cells(i, 4).Select 
    If Cells(i, 4).Value <> "" Then 
        code.... 
    End If 

如何将上述代码修改为我的vba代码,感谢您的建议和帮助,谢谢。

Sub PLANNER()

Dim loopRng As Range
Dim r As Range

Application.ScreenUpdating = False
For Each r In Sheet7.Range("L26", Range("L" & Rows.Count).End(xlUp))
    Sheet7.Range("L19").Value = r.Value
    Set loopRng = Worksheets("setting").Range("L21")
    ActiveWindow.ScrollRow = loopRng
    Application.CutCopyMode = False
    Call planner_Mail
Next r
Sheet2.Range("D2").Select

End Sub

2 个答案:

答案 0 :(得分:1)

我认为应该这样做。

Sub PLANNER()
Dim loopRng As Range
Dim r As Range

Application.ScreenUpdating = False
  For Each r In Sheet7.Range("L26", Range("L" & Rows.Count).End(xlUp))
      If r.Value <> "" Then
          Sheet7.Range("L19").Value = r.Value
      End If
  Set loopRng = Worksheets("setting").Range("L21")
 ActiveWindow.ScrollRow = loopRng
 Application.CutCopyMode = False
  Call planner_Mail
Next r

Sheet2.Range("D2").Select
End Sub

答案 1 :(得分:1)

请参阅下面的代码,代码注释中的解释(以及一些问题):

Option Explicit

Sub PLANNER()

Dim loopRng As Range
Dim R As Range
Dim Sht As Worksheet

Application.ScreenUpdating = False

Set Sht = Sheet7 ' <-- make sure this is the worksheet you have your data
With Sht
    For Each R In .Range("L26", .Range("L" & .Rows.Count).End(xlUp))
        If Trim(R.Value) <> "" Then ' check that cell is not blank
            .Range("L19").Value = R.Value
            Set loopRng = Worksheets("setting").Range("L21") ' Not sure if Sheet7 = Worksheets("setting")
            ActiveWindow.ScrollRow = loopRng
            Application.CutCopyMode = False
            planner_Mail ' you can "drop" the "Call"
        End If
    Next R
End With
Application.ScreenUpdating = True

End Sub