Comment puis-je faire un entier null dans Excel VBA?
Question
J'essaie de détecter si un entier a été défini, et sinon, passez la plupart du code dans la boucle (en utilisant une instruction if). Voici ce que j'ai donc pour.
Do While hws.Cells(r, 9).Value <> ""
On Error Resume Next
ar = Null
ar = aws.Range("A:A").Find(hws.Cells(r, 2).Value).Row
If Not IsNull(ar) Then
'work with ar'
End If
r = r + 1
Loop
Cependant, quand je le lance, ar = Null
a des problèmes. Il dit: "Utilisation non valide de null".
La solution
Find renvoie une gamme:
Dim rf As Range
With aws.Range("A:A")
Set rf = .Find(hws.Cells(r, 2).Value)
If Not rf Is Nothing Then
Debug.Print "Found : " & rf.Address
End If
End With
- http://msdn.microsoft.com /en-us/library/aa195730(office.11).aspx
Autres conseils
Les variables définies comme Integer ne peut pas être NULL dans VBA. Vous devrez trouver une autre façon de faire ce que vous voulez. par exemple, utiliser un autre type de données ou d'utiliser un nombre magique pour indiquer null (par exemple, -1).
Dans votre exemple de code, soit ar sera attribué une valeur à long (Range.Row est longue) ou il une erreur.
il suffit d'utiliser une variante et isempty:
Dim i
If IsEmpty(i) Then MsgBox "IsEmpty"
i = 10
If IsEmpty(i) Then
MsgBox "IsEmpty"
Else
MsgBox "not Empty"
End If
i = Empty
If IsEmpty(i) Then MsgBox "IsEmpty"
'a kind of Nullable behaviour you only can get with a variant
'do you have integer?
Dim j as Integer
j = 0
If j = 0 Then MsgBox "j is 0"