VBA If offset condition

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I am struggling with macro that checks offset of a cell. I am able to build range and everything, but I have problem with IF AND condition.

So looking at mini sheet:
Macro button in column I
Column F - from row 3 to LastRow - input from user
Range N3:T & LastRow - exported data

If condition:
Checks value from column F in column N.
If column O has "X" and column S is not empty and column T contains anything except "X" (can be empty as well), then count how many values meet these conditions and display number in cell J2 with word "found". Then change cell color in column F to red.



Values C, D and E meet these conditions so they are red.

Book1
EFGHIJKLMNOPQRSTU
1
2Macro button3 foundStatusParent Status
3AAXAAX
4BBXBBX
5CCXCC
6DDXDD
7EEXEE
8FFFF
9GGGG
10HH
11II
12JJ
13KKKK
14LLLL
15
Sheet1
 
This macro will work only if you decide which columns are be compared to. As it stands it is according to your layout in attachment Xk2BB and not according to explanations. Adjust the coordinates as needed:
VBA Code:
Option Explicit
Sub CountItems()
    Dim x      As Long
    Dim lr     As Long
    Dim count  As Long
    lr = Range("B" & Rows.count).End(xlUp).Row
    For x = 3 To lr
        If Range("B" & x) = Range("J" & x) Then   'supposed to be: Checks value from column F in column N
            If Range("K" & x) = "X" Then          'supposed to be: If column O has "X"
                If Range("O" & x) <> "" Then      'supposed to be: and column S is not empty
                    If Range("P" & x) <> "X" Then 'supposed to be: and column T contains anything except "X" (can be empty as well)
                        count = count + 1         'then count how many values meet these conditions
                        Range("B" & x).Interior.ColorIndex = 3 'supposed to be: Then change cell color in column F to red
                    End If
                End If
            End If
        End If
    Next x
    Range("F2") = count & " found"                'supposed to be: and display number in cell J2 with word "found"
End Sub
 
Upvote 0
This macro will work only if you decide which columns are be compared to. As it stands it is according to your layout in attachment Xk2BB and not according to explanations. Adjust the coordinates as needed:
VBA Code:
Option Explicit
Sub CountItems()
    Dim x      As Long
    Dim lr     As Long
    Dim count  As Long
    lr = Range("B" & Rows.count).End(xlUp).Row
    For x = 3 To lr
        If Range("B" & x) = Range("J" & x) Then   'supposed to be: Checks value from column F in column N
            If Range("K" & x) = "X" Then          'supposed to be: If column O has "X"
                If Range("O" & x) <> "" Then      'supposed to be: and column S is not empty
                    If Range("P" & x) <> "X" Then 'supposed to be: and column T contains anything except "X" (can be empty as well)
                        count = count + 1         'then count how many values meet these conditions
                        Range("B" & x).Interior.ColorIndex = 3 'supposed to be: Then change cell color in column F to red
                    End If
                End If
            End If
        End If
    Next x
    Range("F2") = count & " found"                'supposed to be: and display number in cell J2 with word "found"
End Sub
Checked and works fine, but there is one small issue - my mistake, I wasn't clear in my post - data in column F doesn't always match the length of data in column N.
 
Upvote 0
Why ? how can the difference be determined. At the moment the macro expects the cells to be identical in content, a simple extra space or upper/lower case determines a difference.
Please attach some 'real' examples and explain what should match and what not.
 
Upvote 0
Why ? how can the difference be determined. At the moment the macro expects the cells to be identical in content, a simple extra space or upper/lower case determines a difference.
Please attach some 'real' examples and explain what should match and what not.
Yes, sorry. This is how whole report looks like:

In column F there is a list of values that need to be checked (unique values, no duplicates).
Export is pasted in column AE - AQ (starting from row 3, different length every time users uses this report)
Now I need to check value from column F - find it in column AE and determine if column AH has "X", column AO is not empty and if column AP has anything else except "X" (can be blank, can be B, can be anything but no "X").

If value meets all of these conditions, then it is highlighted red in column F and counter in cell J2 shows how many values are found.

Values in column AE are sometimes duplicated, but if any of them meet the conditions, it needs to be counted as well.



So in this case if you look at row 3 in columns AE - AQ, 9F0001990 has "X" in column AH, column AO is not empty and column AP is not "X". So this value meets all conditions, so it should be highlighted in column F.

I hope everything is clear now.

OB.xlsm
FGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQ
1Materials checkStatusZOBSOLETE - STATUS
2All listedMaterialPlantX-plant matl statusPlant-sp.matl statusDF at client levelDF at plant levelFollow-Up MaterialEffective-out dateTotal StockReserve QuantityParentParent statusPreceding Material(s)
39F0001990VB02 - LISTING(9F only)VD52 - CMIR(9F only)9F00019904020X009F0006678
49F00019919F00019909010X009F0006677X
59F00019929F00019914020X009F0006677X
69F00019939F00019919010X00
79F00019949F00019924020X00
89F00019959F00019929010X00
99F00019969F00019934020X00
109F00019979F00019939010X00
119F00019989F00019944020X00
129F00019999F00019949010X00
139F00020009F00019954020X00
149F00020019F00019959010X00
159F00020029F00019964020X00
169F00020039F00020039010X00
179F00020049F00019974020X00
189F00020059F00019979010X00
199F00020069F00019984020X00
209F00020079F00019989010X009F0006677X
219F00020089F00019994020X00
229F00020099F00019999010X00
239P00052239F00020004020X00
249P00052249F00020009010X00
259P0005225VK12 - ZPR0, ZVOL, MPG(9F only)C223, QSR6 - ROUTINGS(9F, 9W99, 9W00 only)9F00020014020X00
269P00052269F00020019010X00
279P00052279F00020024020X00
289P00052289F00020049010X00
299P00052294020X00
309P00052309010X00
319P00052314020X00
329P00052329010X00
339P00052339F00020054020X00
349P00052349F00020059010X00
359P00052359F00020064020X00
369P00052369F00020069010X00
379P00052379F00020074020X00
389P00052389F00020079010X00
399P00052399F00020084020X00
409W99023619F00020089010X00
419W99023629F00020094020X00
429W99015979F00020099010X00
439W99015239P00052234020X9P0005223A00
449W99015999P00052239010X9P0005223A00
459W99016179P00052239040X00
469W99016009P00052244020X32509P0004940A
479W99016019P00052249010X009P0004940A
489W99016079P00052249040X009P0004940A
499W99016139P00052254020X00
509W99016149P00052264020X00
519W99016159P00052269010X00
529W99016169P00052269040X00
539W99016199P00052274020X00
549W99015349P00052284020X009F0001990OB
559W99016219P00052284020X009W9902648OB
569W99016229P00052294020X009W0000238OB
579W99016239P00052299010X00
589W99015659P00052299040X00
599W99015799P00052304020X00
609W00001759P00052309010X00
619W00001769P00052309040X00
Set to obsolete
 
Last edited:
Upvote 0
You once again changed the layout of your sheet !!!
Added a new loop to double-check on duplicates in column AE.
VBA Code:
Option Explicit
Sub CountItems_Dup()
    Dim x      As Long
    Dim y      As Long
    Dim lrF    As Long
    Dim lrAE   As Long
    Dim count  As Long
    lrF = Range("F" & Rows.count).End(xlUp).Row
    lrAE = Range("AE" & Rows.count).End(xlUp).Row
    For x = 3 To lrF
        For y = 3 To lrAE
            If Range("F" & x) = Range("AE" & y) Then
                If UCase(Range("AH" & y)) = "X" Then
                    If Range("AO" & y) <> "" Then
                        If UCase(Range("AP" & y)) <> "X" Then
                            count = count + 1
                            Range("F" & x).Interior.ColorIndex = 3
                        End If
                    End If
                End If
            End If
        Next y
    Next x
    Range("J2") = count & " found"
End Sub
 
Upvote 0
Solution
You once again changed the layout of your sheet !!!
Added a new loop to double-check on duplicates in column AE.
VBA Code:
Option Explicit
Sub CountItems_Dup()
    Dim x      As Long
    Dim y      As Long
    Dim lrF    As Long
    Dim lrAE   As Long
    Dim count  As Long
    lrF = Range("F" & Rows.count).End(xlUp).Row
    lrAE = Range("AE" & Rows.count).End(xlUp).Row
    For x = 3 To lrF
        For y = 3 To lrAE
            If Range("F" & x) = Range("AE" & y) Then
                If UCase(Range("AH" & y)) = "X" Then
                    If Range("AO" & y) <> "" Then
                        If UCase(Range("AP" & y)) <> "X" Then
                            count = count + 1
                            Range("F" & x).Interior.ColorIndex = 3
                        End If
                    End If
                End If
            End If
        Next y
    Next x
    Range("J2") = count & " found"
End Sub
I am sorry, but what do you mean by once again? I used blank sheet as an example and I pointed to correct columns. Correct me please if I am wrong.

And thank you, this code works for me, thanks for help!

1666165276672.png
 
Upvote 0
Now I need to check value from column F - find it in column AE and determine if column AH has "X", column AO is not empty and if column AP has anything else except "X" (can be blank, can be B, can be anything but no "X").
This quoted sentence is from your post #5 and it doesn't seem to match your post #7.
Anyway, thanks for the positive feedback, glad having been of some help(y).
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top