简体   繁体   中英

How to check dependency in excel columns and display error in excel-vba?

I have a series of columns in my excel. For the sake of simplicity, lets consider it Potato, Tomato, Currency1, Amount1, Currency2, Amount2 ... Currency10, Amount10

If Currency1 or Amount1is empty; then it should fill both the cells in red color else; it should ignore those cells.

I have tried the following thing. However, its not working out for my case and I have to include a lot of iterations. Please suggest a better way if possible.

My code is as follows:

' Function to get last column
cols = LastColumnInOneRow()
' Function to get last row
Rows = fnLastRowOfAColumn(cols)

For num_cols = 1 To cols
    For num_rows = 2 To Rows
        cell_value = Cells(num_rows, num_cols)
        Set selected_attr = Cells(1, num_cols)
        Select Case selected_attr
            Case Is = "Currency1"
                If Cells(num_rows + 1, num_cols) <> "" Then
                    Cells(num_rows, num_cols).Interior.ColorIndex = 0
                Else
                    Cells(num_rows, num_cols).Interior.ColorIndex = 3
                End If

            Case Is = "Amount1"
                If Cells(num_rows - 1, num_cols) <> "" Then
                    Cells(num_rows, num_cols).Interior.ColorIndex = 0
                Else
                    Cells(num_rows, num_cols).Interior.ColorIndex = 3
                End If

            ' Till end of sheet ...
        End Select
    Next num_rows '+ 1
Next num_cols '+ 1

I am sorry I cannot comment but I can give you the following tip:

You can eventually set up conditional formatting as a vba code, here is an example to set your range to red if not empty as a conditional formatting:

Range("A2", Cells(Rows, Cols)).FormatConditions.Delete 'this would eventually delete previous formating
With Range("A2", Cells(Rows, Cols)).FormatConditions.Add(xlCellValue, xlNotEqual, "=" & Chr(34) & Chr(34))
    .Interior.ColorIndex  = 3
End With

Edit: here is an example to set your range to green if your cell is equal to 'Currency1' and the cell bellow is empty as a conditional formatting:

With Range("A2", Cells(Rows, Cols)).FormatConditions.Add(xlExpression, , "= AND(A3<>" & Chr(34) & Chr(34) & "; A2=" & Chr(34) & "Currency1" & Chr(34) & ")")
    .Interior.ColorIndex  = 4
End With

I solved my that scenario using the following in Amount1, Amount2 .. Amount10:

                If IsEmpty(Cells(num_rows, num_cols + 1)) = True And IsEmpty(Cells(num_rows, num_cols)) = True Then

                    If .Test(cell_value) = False Then
                        Cells(num_rows, num_cols).Interior.ColorIndex = 3
                    Else
                        Cells(num_rows, num_cols).Interior.ColorIndex = 0
                    End If
                    Set RegEx = Nothing
                    End With
                ElseIf (IsEmpty(Cells(num_rows, num_cols + 1)) = False And IsEmpty(Cells(num_rows, num_cols)) = True) Then
                    Cells(num_rows, num_cols).Interior.ColorIndex = 3
                    Cells(num_rows, num_cols + 1).Interior.ColorIndex = 0
                ElseIf (IsEmpty(Cells(num_rows, num_cols + 1)) = True And IsEmpty(Cells(num_rows, num_cols)) = False) Then
                    Cells(num_rows, num_cols + 1).Interior.ColorIndex = 3
                    Cells(num_rows, num_cols).Interior.ColorIndex = 0
                Else
                    Cells(num_rows, num_cols).Interior.ColorIndex = 0
                    Cells(num_rows, num_cols + 1).Interior.ColorIndex = 0
                End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM