PDA

View Full Version : Solved: Need help with VBA syntax



morty
11-04-2010, 09:32 AM
I do not have much experience in VBA so I was wondering if someone can help me with the syntax of the following problem.

Here is an example table I have:
hxxp://imgur.com/Zlwbm.png (first post so I can't post links or images)

I want to have a cell display a warning if more than 1 cells in column B are "yes" AND the cells in column A (within the same rows of those cells in B that are "yes") are different dates.

Nothing needs to happen if the dates are the same and nothing needs to happen if the cells in B are "no"

I think I may have an idea how to do this but I'm not familiar with VBA syntax if someone could help me with this.


Private Sub Worksheet_Change(ByVal Target As Range)

For each cell in column B
If cell.value = "yes" Then
array += cell.address
End If
End For

If count(array) > 1 Then
For each item in array
array2 += adjacent cell in column A
End For
End If

If array2 has unique values AND range("H6").value is null Then
Range("H6").Value = "Warning: message here"
Else If array2 does not have unique AND range("H6").value is not null Then
Range("H6").Value = null
Else If array2 is null Then
Range("H6").Value = null
End If

End Sub

Bob Phillips
11-04-2010, 09:52 AM
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nMatches As Long

If Not Intersect(Target, Me.Range("A:B")) Is Nothing Then

nMatches = Application.Evaluate("=COUNT(1/FREQUENCY(IF(B2:B20=""yes"",IF(A2:A20<>"""",A2:A20)),IF(B2:B20=""yes"",IF(A2:A20<>"""",A2:A20))))")
If nMatches > 1 Then MsgBox "Multiples"
End If

End Sub

morty
11-04-2010, 11:25 AM
I can't seem to get this to work.

Nothing happens at all.

morty
11-04-2010, 12:15 PM
Is there a way to do this:

For Each cell as Range("B1:B10")
If cell.Value = "yes" Then
'This is where I need to dump the adjacent cell's value into an array and don't know how
End If
Next

'This is where I need to check if the array has more than 1 distinct unique value

Bob Phillips
11-04-2010, 12:41 PM
Maybe it should be



Private Sub Worksheet_Change(ByVal Target As Range)
Dim nMatches As Long

If Not Intersect(Target, Me.Range("A:A, H:H")) Is Nothing Then

nMatches = Application.Evaluate("=COUNT(1/FREQUENCY(IF(B2:B20=""yes"",IF(H2:H20<>"""",H2:H20)),IF(B2:B20=""yes"",IF(H2:H20<>"""",H2:H20))))")
If nMatches > 1 Then MsgBox "Multiples"
End If

End Sub

mikerickson
11-04-2010, 03:56 PM
Perhaps
Private Sub Worksheet_Change(ByVal Target As Range)
Dim bRange As Range
Dim fStr As String
Dim countYes As Long

With ActiveSheet
Set bRange = Range(.Cells(1, 2), .Cells(.Rows.Count, 2).End(xlUp))
End With

With bRange
fStr = .Offset(0, -1).Address(, , , True) & ",--(" & .Address(, , , True) & "=""yes"")"
fStr = "Sumproduct(" & fStr & ")"

countYes = Application.CountIf(bRange, "yes")

On Error Resume Next
If Evaluate(fStr) = countYes * .Offset(0, -1).Cells(Application.Match("yes", .Cells, 0), 1) Then

Else
MsgBox "message"
End If
On Error GoTo 0
End With
End Sub

morty
11-05-2010, 07:49 AM
I appreciate all the help in this thread, I guess I just didn't understand what your code is doing so I couldn't quite get it to work.

Anyway, this is what I ended up putting together after way to long of a time trying to figure out collections.


Function inColl(value As Variant, coll As Collection)
For Each v In coll
If value = v Then
inColl = True
ElseIf value <> v Then
inColl = False
End If
Next
End Function

Private Sub Worksheet_Change(ByVal target As Range)
Dim coll As New Collection
For Each c In Range("U4:U10")
If c.value = "Yes" And Range(c.Address).Offset(0, -1).value <> "" And inColl(Range(c.Address).Offset(0, -1).value, coll) = False Then
coll.Add Range(c.Address).Offset(0, -1).value
End If
Next

If coll.Count > 1 And Range("L20").value = "" Then
Range("L20").value = "Warning: Multiple dates in today's prepayment"
ElseIf coll.Count <= 1 And Range("L20").value <> "" Then
Range("L20").value = ""
End If
End Sub

Bob Phillips
11-05-2010, 08:11 AM
So are you sorted?

morty
11-05-2010, 08:17 AM
I am, you can mark as solved, thank you.

Bob Phillips
11-05-2010, 10:52 AM
You do the marking, not me.

mdmackillop
11-06-2010, 06:39 AM
Hi Morty
This code in effect only checks the last item in the collection as it overwrites previous tests in each loop.
BTW, you should really use Option Explicit and declare all your variables.
Function inColl(value As Variant, coll As Collection)
Dim v
For Each v In coll
If value = v Then
inColl = True
ElseIf value <> v Then
inColl = False
End If
Next
End Function