¿Cómo hago un número entero de cero en Excel VBA?
Pregunta
Estoy tratando de detectar si se estableció un entero, y si no, saltar la mayor parte del código del bucle (usando una sentencia if). Aquí es lo que tengo hasta para.
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
Sin embargo, cuando lo ejecuto, ar = Null
tiene problemas. Se dice "Uso no válido de null".
Solución
Find devuelve un rango:
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
Otros consejos
Las variables definidas como entero no puede ser nulo en VBA. Usted tendrá que encontrar otra manera de hacer lo que quiera. por ejemplo, utilizar un tipo de datos diferente o utilizar un número mágico para indicar nula (por ejemplo, -1).
En el código de ejemplo, o bien ar se le asignará un valor de largo (Range.Row es un Long) o se generará un error.
sólo tiene que utilizar una variante y 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"