在vba中,使用&遍历多个范围的正确语法是什么

时间:2019-03-06 13:41:54

标签: vba

我想遍历代码以执行以下操作:如果E列中有一个空单元格,我想突出显示从A到H的整个行。我尝试使用'&'将数字替换为变量如下所示,但失败了。使用VBA的语法是什么?

 Sub CheckForBlanksInName()

    Dim myRange As Range
    Set myRange = Sheet1.Range("A2:H103")
    Dim rowrng As Range

    'clear all color
    myRange.Interior.ColorIndex = xlNone

    'Last Row
    last_row = Range("A2").End(xlDown).Row 'Last row of the data set
    MsgBox last_row '=> returns
    For 

   rownum = 2 To last_row - 1
   If IsEmpty(Range("E" & rownum)) = True Then
        'myRange(("A & rownum : H" & rownum)).Interior.Color = RGB(166, 166,
      166) 'Gray DID NOT WORK

     ' Set rowrng = Range("A & rownum : H" & rownum)
        rowrng.Interior.Color = RGB(166, 166, 166)  'DID NOT WORK

     'Cell E is blank
          MsgBox rownum & "Cell E", rownum
   End If
   Next rownum
   End Sub

4 个答案:

答案 0 :(得分:0)

您的代码中有几个语法错误。您可以执行以下操作:

Sub CheckForBlanksInName()
        Dim myRange As Range
        Set myRange = Sheets("Sheet1").Range("A2:H103") '<~~ use sheet name instead
        myRange.Interior.ColorIndex = xlNone            '<~~ clear color
    '------
        Dim last_row As Long                            '<~~ declare last_row
        last_row = Range("A2").End(xlDown).Row          '<~~ fix last_row
    '------
        For rownum = 2 To last_row - 1
            If IsEmpty(Sheets("Sheet1").Range("E" & rownum)) = True Then '<~~ use complete addresses
                Sheets("Sheet1").Range("A" & rownum & ":H" & rownum).Interior.Color = RGB(166, 166, 166) '<~~ use complete addresses
            End If
        Next rownum
End Sub

答案 1 :(得分:0)

或者只是循环浏览

Sub CheckForBlanksInName2()

   Dim rng As Range, roww As Range
   Set rng = ThisWorkbook.Sheets(1).Range("A2:H103")
       rng.Interior.ColorIndex = xlNone

   For Each roww In rng.Rows
       If IsEmpty(roww.Cells(1, 5)) Then roww.Interior.ColorIndex = 50
   Next

End Sub

答案 2 :(得分:0)

您只需在“ E” Cells中的特定Column中循环,并根据其isEmpty()函数结果的结果,即可随意突出显示它们。

Dim cell As Range
Dim ws as Worksheet: Set ws = Sheets("Sheet1") ' <- change to whatever sheet you're using
Dim lr as Long: lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

For Each cell In ws.Range("E1:E" & lr)
    If IsEmpty(cell) Then
        cell.EntireRow.Interior.ColorIndex = 27
    End If
Next cell

产生预期的结果

enter image description here

答案 3 :(得分:0)

1。)循环语法:For ... To应该在同一行上。

2。)范围语法:您尝试使用“ myRange((” A&rownum:H“&rownum))”是     指“ myrange”范围对象内的范围。可以做到,但是     您在这里不需要它,这不是正确的语法。参考:     http://www.cpearson.com/excel/cells.htm

有以下几种方法可以引用要突出显示的范围:

sheet1.Range(“ A”&rownum&“:H”&rownum)

sheet1.Range(“ A”&rownum.row&“:H”&rownum.row)

sheet1.Range(“ A”和rownum,sheet1.range(“ A”和rownum).offset(0,7))

sheet1.Range(sheet1.Cells(rownum,“ A”),sheet1.Cells(rownum,“ H”))

sheet1.Range(sheet1.Range(“ A”和rownum),sheet1.Cells(rownum,“ H”))

在引用范围时,VBA允许很多变化,但有一些技巧:
    1.完全符合您的图纸参考资料[例如-sheet1.Range(sheet1 ...)]     2.小​​心单调的报价     3.使用With ... End With块

3。)如果您的空白单元格中的last_row变量将不可靠     数据,因为它将返回任何空白单元格之前的最后一行。见下文

4。)您不需要循环即可完成此操作;我建议改用自动过滤器。

这是我要怎么做:

   Sub CheckForBlanksInName()
    Dim myRange As Range
    Dim last_row As Double

    With Sheet1

      last_row = .Cells(.Rows.Count, "A").End(xlUp).Row 'Last row of the data set
      MsgBox last_row '=> returns
      Set myRange = .Range("A2:H" & last_row)

      'clear all color
      myRange.Interior.ColorIndex = xlNone

      .AutoFilterMode = False  'clear any filters
      .Range("E1:E" & last_row) _ 
        .AutoFilter _
            field:=1 _
          , Criteria1:="=" _
          , Operator:=xlFilterValues  'filter column "E" on blank cells

      'Highlight
      myRange.SpecialCells(xlCellTypeVisible).Interior.Color = vbYellow

      .AutoFilterMode = False  'clear filters

    End With
   End Sub