条件格式化单元格错误

时间:2018-03-07 12:52:06

标签: excel vba

我下面的代码是检查两个工作表,以查看在特定列中插入的值是否相似。例如,它查看从sheet1 中插入列A中的值是否与 sheet2列B 中插入的值相同。如果是,那么 sheet1列A 中的单元格仍然是白色'否则,他们会变成红色'。代码工作没有任何问题,而且非常快。

我的问题如下。让我们说:

  • 我需要在 sheet1 - A列中插入一个值,单元格A2到A5与 sheet2 B列中的值匹配。
  • sheet2列B 具有以下值:汽车,房屋,花园,城市,国家。
  • 如果在A2中我写汽车,A3我留空,A4 国家和A5 汽车,那么A2,A4和A5将保持白色'因为这些值在 sheet2 - B列中。但是,即使单元格为空, A3也会变红 - 这就是我的问题。如果该单元格为空,如何使代码不考虑?它不应该变成红色,因为我将单元格留空并且没有比较任何东西......

我希望我能以某种方式解释自己。谢谢你的帮助!

class Movies extends Component {
  render() {
    let movieItems = this.props.movies.map(movie => {
      return <MovieItem key={movie.id} movie={movie} />;
    });
    return <div style={flex}>{movieItems}</div>;
  }
}

class Movies extends Component {
  render() {
    return (
      <div style={flex}>
        this.props.movies.map((movie)=>{
          <MovieItem key={movie.id} movie={movie} />
        }
      </div>
    );
  }
}

1 个答案:

答案 0 :(得分:1)

喜欢?

Private Sub CommandButton1_Click()

Set wb = Excel.ActiveWorkbook
Set aRec = wb.Worksheets(1)
Set bRec = wb.Worksheets(2)

Application.ScreenUpdating = False

For a = 2 To aRec.Cells(Rows.Count, "A").End(xlUp).Row
    Match = Application.Match(aRec.Cells(a, 1).Value, bRec.Columns(2), 0)

    If IsError(Match) And Not IsEmpty(aRec.Cells(a, 1)) Then
        aRec.Cells(a, 1).Interior.Color = RGB(255, 0, 0)

    Else
        aRec.Cells(a, 1).Interior.Color = RGB(255, 255, 255)

    End If

Next a

End Sub

使用正确的循环变量,Option Explicit,键入声明并重新开启screenupdating on

Option Explicit

Private Sub CommandButton1_Click()

Dim wb As Workbook
Dim aRec As Worksheet
Dim bRec As Worksheet
Dim a As Long
Dim Match As Variant

Set wb = ActiveWorkbook
Set aRec = wb.Worksheets(1)
Set bRec = wb.Worksheets(2)

Application.ScreenUpdating = False

For a = 2 To aRec.Cells(Rows.Count, "A").End(xlUp).Row

    Match = Application.Match(aRec.Cells(a, 1).Value, bRec.Columns(2), 0)

    If IsError(Match) And Not IsEmpty(aRec.Cells(a, 1)) Then
        aRec.Cells(a, 1).Interior.Color = RGB(255, 0, 0)
    Else
       aRec.Cells(a, 1).Interior.Color = RGB(255, 255, 255)
    End If

Next a

Application.ScreenUpdating = True

End Sub