I have a set of rows and columns range is A:AC and Rows count may be anything
Fixing the sheet and range and calculating RowCount and ColumnCount using this
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)
- First Condition:
I am trying to compare column F to AC should have either 0 or 1. and found that by using
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
- Second Condition
Out of these columns I should have only one column should have 1 and remaining as 0
I tried this and it is throwing different error codes for each run
- Third Condition
Find duplicate rows from A:AC
I need to highlight entire row which has the error
But I have done which Column has error
with this statement
Cells(i, j).Interior.Color = vbRed
Set selectedRange = Range("A1:AC" & LastRow)
is selecting a range on the active sheet, not on the sheet your other statements are accessing. You probably want to saySet selectedRange = sheet.Range("A1:AC" & LastRow)
- but you don't appear to be using selectedRange elsewhere so it probably doesn't matter. (b) Similarly, all the references toCells
in your "first condition" block are accessing the active sheet - you probably needsheet.Cells
. - YowE3K