B
B
Bartiwka2018-05-28 18:04:51
Microsoft
Bartiwka, 2018-05-28 18:04:51

How to fix the error when the conditions are met? VBA, Excel?

Good afternoon! Let me explain the essence of the problem! I tried to implement a condition that must be met:
if the previous cell is green, then you need to insert a value into the current cell, but then an error occurs that I don’t understand and may not have googled correctly (in short, I didn’t find the answer). I ask you for help! For complete clarity, I provide a screenshot of the table and the actual code itself. Most likely I missed something or did not take into account! I will be grateful for your help!5b0c19f8d6c6b693207784.png

Sub perebor()
Dim sh As Range
Dim i As Long
Dim j As Long
Dim bh As Range

       Set wbs = ThisWorkbook.Worksheets("table1")
       Set WBT = ThisWorkbook.Worksheets("mnd")

i = 3
j = 9
For Each sh In wbs.Range("N3:N71")
For Each bh In wbs.Range("M3:M71")
'белый -- 16777215
'серый -- 13421772
' зеленый -- 5880731
' оранжевый -- 4626167
If sh.Interior.Color = 16777215 Then



If bh.Interior.Color = 5880731 Then
j = j - 1

wbs.Cells(i, 14).Value = WBT.Cells(j, 1).Value
wbs.Cells(i, 15).Value = WBT.Cells(j, 1).Value
wbs.Cells(i, 14).Interior.Color = WBT.Cells(j, 1).Interior.Color
wbs.Cells(i, 15).Interior.Color = WBT.Cells(j, 1).Interior.Color


i = i + 1
End If

'wbs.Cells(i, 14).Value = WBT.Cells(j, 1).Value
'wbs.Cells(i, 15).Value = WBT.Cells(j, 1).Value
'wbs.Cells(i, 14).Interior.Color = WBT.Cells(j, 1).Interior.Color
'wbs.Cells(i, 15).Interior.Color = WBT.Cells(j, 1).Interior.Color



ElseIf sh.Interior.Color = 13421772 Then
wbs.Cells(i, 14).Value = wbs.Cells(i, 14).Value

i = i + 1

End If

Next bh

Next sh


End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2018-05-29
@Dr_Elvis

The error code does not issue, but does not fulfill the conditions.
for example, I got a working version, but in one place you take the value of the cell and insert it there, which is not clear why (I noted (???))

Sub perebor()
    Dim i As Long
    Dim j As Long
    Dim bh As Range
    Set wbs = ThisWorkbook.Worksheets("table1")
    Set WBT = ThisWorkbook.Worksheets("mnd")
    i = 3
    j = 9
        For Each bh In wbs.Range("M3:M11")
            'белый -- 16777215
            'серый -- 13421772
            ' зеленый --  5880731
            ' оранжевый -- 4626167
            mcolor = wbs.Cells(i, 13).Interior.Color
            ncolor = wbs.Cells(i, 14).Interior.Color
            If ncolor = 16777215 And mcolor = 5880731 Then
                    j = j - 1
                    wbs.Cells(i, 14).Value = WBT.Cells(j, 1).Value
                    wbs.Cells(i, 15).Value = WBT.Cells(j, 1).Value
                    wbs.Cells(i, 14).Interior.Color = WBT.Cells(j, 1).Interior.Color
                    wbs.Cells(i, 15).Interior.Color = WBT.Cells(j, 1).Interior.Color
            ElseIf ncolor = 13421772 Then
                wbs.Cells(i, 14).Value = wbs.Cells(i, 14).Value '(???)
            End If
            i = i + 1
        Next bh
End Sub

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question