AND Condition

sevenfive85

New Member
Joined
Jul 21, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Can someone please help with this conditional statement? I need two things to be true for the word "Select" to appear, Cell D3 must be "Yes" and C8 must contain something. Code below is what I have but it is wrong, please help. Thank you.


If Target.Address = "$D$3" Then
If Target.Value = "Yes" And Range("C8").Value <> "" Then
Range("K8:K300").Value = "Select"
Else
Range("K8:K300").Value = ""
End If
End If

1628109684755.png
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How is it wrong?
Can you post the complete code? And explain how you expect it to work.
And try to avoid merged cells unless you really need them they ca be apita sometimes. ;)
 
Upvote 0
I presume this is in your worksheet change event code so try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$3" Then
    Application.EnableEvents = False
   
    Range("K8:K300").Value = ""
    If Target.Value = "Yes" Then
        inarr = Range("c8:c300")
        outarr = Range("K8:K300").Value
        For i = 1 To UBound(inarr, 1)
         If inarr(i, 1) <> "" Then
           outarr(i, 1) = "Select"
         End If
        Next i
        Range("K8:K300").Value = outarr
    End If
    Application.EnableEvents = True

End If
End Sub
 
Upvote 0
Solution
Aaah, i think i get it.
You need Kx to display Select if you have a value in Cx. But the problem is you only check for C8.
So you need to loop through all C cells and then set a value for each K cell. Or you can do with a simple formula in column K.
 
Upvote 0
VBA Code:
If Target.Address = "$D$3" Then
 If Target.Value = "Yes" Then
  For each cc in Range("C8:C300")
   If cc.Value <> "" Then
    Range("K"&cc.row).Value = "Select"
   Else
    Range("K"&cc.row).Value = ""
   End If
  Next cc
 Else
  Range("K8:K300").Value = ""
 End if
End If
Or in K8
Excel Formula:
=If(And($D$3="Yes",C8<>""),"Select","")
 
Upvote 0
BTW the code from offthelip will probably work much faster. Especially over larger datasets.
 
Upvote 0
Here is the complete code. Actually it is working but not the way I was hoping. It is checking cell C8 only and populating the word "select" from K8 to K300 regardless if the C column is empty. I need it to check cell C9, C10, C11,....etc. Empty cell in the C column should not have "select" on the K column. Hopefully that clear things up.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$3" Then
If Target.Value = "Yes" And Range("C8").Value <> "" Then
Range("K8:K300").Value = "Select"
Else
Range("K8:K300").Value = ""
End If
End If
End Sub
 
Upvote 0
Have you tried my code from post 3 I think it does exactly what you have requested and will be very fast, which is necessary in a worksheet change event otherwise it slows everything down on your workhseet
 
Upvote 0
Have you tried my code from post 3 I think it does exactly what you have requested and will be very fast, which is necessary in a worksheet change event otherwise it slows everything down on your workhseet
thank you guys!!! Both solution work perfectly
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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