Excel宏VBA基本比较 - 但无法修复错误

时间:2016-10-12 07:52:40

标签: excel vba excel-vba

我有一组行和列的范围是 A:AC ,行数可能是任何

修复工作表和范围并使用此计算RowCount和ColumnCount

Dim sheet As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Set sheet = ActiveWorkbook.Worksheets(1)
LastRow = sheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = sheet.Range("A1").CurrentRegion.Columns.Count
Set selectedRange = Range("A1:AC" & LastRow)
  • 第一个条件:

我试图比较F列和AC应该有0或1.并且发现使用

For i =1 To LastRow
   For j = 6 To LastColumn
      If Cells(i, j).Value <> 0 And Cells(i, j) <> 1 Then
           Cells(i, j).Interior.Color = vbGreen
      End If
      If Cells(i, j).Value > 1 Then
           Cells(i, j).Interior.Color = vbRed
      End If
   Next j
Next i
  • 第二个条件

在这些专栏中,我应该只有一列应该 1 并保持 0

我试过了,每次运行都会抛出不同的错误代码

  • 第三个条件

从A:AC

中查找重复行

我需要突出显示错误

的整个

But I have done which Column has error 此声明 的 Cells(i, j).Interior.Color = vbRed

虽然很简单但我无法弄清楚逻辑,因为我对Excel VBA完全不熟悉。

1 个答案:

答案 0 :(得分:1)

对于你的第一个条件:

For i = 1 To LastRow
    For j = 6 To LastColumn
        Select Case Cells(i, j).Value
            Case 0
                Cells(i, j).EntireRow.Interior.Color = vbGreen
            Case 1
                Cells(i, j).EntireRow.Interior.Color = vbRed

            Case Else
                Cells(i, j) = "Whetever"
        End Select
        if range("A" & i).value = 1 and range("AC" & i).value = 1 then
            Cells(i, j).EntireRow.Interior.Color = vbYellow
        end if
   Next j
Next i

现在添加了第2点。要删除重复项,这取决于您的数据,但问题已在此处提出:

How to look for repeated rows and then delete one of them? requires VBA

相关问题