Problem with vba code

Ciccio86

New Member
Joined
Feb 3, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hi i have problem with this code. Can someone help me?
Im tryng to check if value on the left cell from checkbox in sheet "View" is in a table on another sheet called "List" and if his date is expired... but every time i have the same result.

VBA Code:
Sub Listing()

Dim ViewSheet As Worksheet
Dim ListSheet As Worksheet
Dim ViewValue As Variant
Dim ValueFound As Boolean
Dim ValueExpired As Boolean
Dim chk As oleObject

Set ViewSheet = ThisWorkbook.Sheets("View")
Set ListSheet = ThisWorkbook.Sheets("List")

If TypeName(chk) = "CheckBox" Then
ViewValue = ViewSheet.Cells(chk.TopLeftCell.Offset(0, -1)).value

ValueFound = False
ValueExpired = False

For i = 1 To ListSheet.Range("A1").End(xlDown).Row
If ListSheet.Range("B" & i).value = ViewValue Then
ValueFound = True
If ListSheet.Range("C" & i).value < Date Then
ValueExpired = True
Else
ValueExpired = False
End If
End If
Next i
End If

If ValueFound = True And ValueExpired = False Then
chk.TopLeftCell.Offset(0, 1)).value = "OK"
Else
MsgBox "Not in the List"
chk.Object = False
chk.TopLeftCell.Offset(0, 1)).value = "NOT OK"
End If

End Sub

Cattura.JPG
 
That is a different issue.
Try the below but i if doesn't work you can log it as a seperate thread.

Where you have the click event put the below, make sure you include the Public line at the top.
VBA Code:
Public runChkBox As Boolean

Private Sub CheckBox1_Click()
    Dim chk As OLEObject
    Set chk = Me.OLEObjects(CheckBox1.Name)
    If runChkBox Then
        runChkBox = False
        Exit Sub
    End If
   
    If Listing(chk) = "Not Found" Then
        runChkBox = True
        chk.Object = False
    End If

End Sub

I have changed the Listing Sub to a function so replace it with this.
VBA Code:
Function Listing(chk As OLEObject)

Dim ViewSheet As Worksheet
Dim ListSheet As Worksheet
Dim ViewValue As Variant
Dim ValueFound As Boolean
Dim ValueExpired As Boolean
Dim i As Long

    Set ViewSheet = ThisWorkbook.Sheets("View")
    Set ListSheet = ThisWorkbook.Sheets("List")
   
    If TypeName(chk.Object) = "CheckBox" Then
        ViewValue = chk.TopLeftCell.Offset(0, -1).Value
       
        ValueFound = False
        ValueExpired = False
       
        For i = 1 To ListSheet.Range("A1").End(xlDown).Row
            If ListSheet.Range("B" & i).Value = ViewValue Then
                ValueFound = True
                If ListSheet.Range("C" & i).Value < Date Then
                    ValueExpired = True
                Else
                    ValueExpired = False
                End If
            End If
        Next i
    End If
   
    If ValueFound = True And ValueExpired = False Then
        chk.TopLeftCell.Offset(0, 1).Value = "OK"
    Else
        MsgBox "Not in the List"
        Listing = "Not Found"
        chk.TopLeftCell.Offset(0, 1).Value = "NOT OK"
    End If

End Function
Thanks
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You're welcome.
I would be interested to know if my post # then 10 fixes the issue of the code changing the checkbox calling the click event up again and effectively running the code a 2nd time.
If it doesn't and you open a new thread for that issue drop the link here and I will have a look to see what others come up with.
 
Upvote 0
You're welcome.
I would be interested to know if my post # then 10 fixes the issue of the code changing the checkbox calling the click event up again and effectively running the code a 2nd time.
If it doesn't and you open a new thread for that issue drop the link here and I will have a look to see what others come up with.
Yes it fix
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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