Problema con el método Range.Find
Les escribo nuevamente para solicitar su ayuda con problema que me tiene algo desubicado, el código es el siguiente:
Sub pruebas()
Dim Fecha As Date, Rango As Range, RangoF As Range, FechaFinal As Integer, FechaInicio As Integer, RangoC As Range
Set Rango = Sheet2.ListObjects("Table1").ListColumns(2).Range
TurnoX = Sheet1.Cells(Rows.Count, "G").End(xlUp).Value
Fecha = Date - 1 'Format(Date, "MMDDYYYY")
FechaInicio = Rango.Find(Fecha, LookIn:=xlValues, searchdirection:=xlNext).Row
FechaFinal = Rango.Find(Fecha, LookIn:=xlValues, searchdirection:=xlPrevious).Row
RangoF = Range(Cells(FechaInicio, 6), (Cells(FechaFinal, 6)))
RangoC = Range(Cells(FechaInicio, 3), (Cells(FechaFinal, 3)))
TurnoEnRango = WorksheetFunction.CountIf(RangoC, Turno) '''coincidencias encontradas del turno actual en la fecha actual
Coincidencias1 = WorksheetFunction.CountIf(RangoF, 1) '''coincidencias encontradas de la fecha actual de la maquina 1
Coincidencias2 = WorksheetFunction.CountIf(RangoF, 2) '''coincidencias encontradas de la fecha actual de la maquina 2
If UserForm1.OptionButton1.Value = True Then
If Coincidencias1 >= 1 And TurnoEnRango >= 1 Then
UserForm1.OptionButton5.Value = True
UserForm1.OptionButton5.Enabled = False
UserForm1.OptionButton6.Enabled = False
Else
UserForm1.OptionButton5.Value = False
UserForm1.OptionButton5.Enabled = True
UserForm1.OptionButton6.Enabled = True
End If
Else
If UserForm1.OptionButton2.Value = True Then
If Coincidencias2 >= 1 And TurnoEnRango >= 1 Then
UserForm1.OptionButton5.Value = True
UserForm1.OptionButton5.Enabled = False
UserForm1.OptionButton6.Enabled = False
Else
UserForm1.OptionButton5.Value = False
UserForm1.OptionButton5.Enabled = True
UserForm1.OptionButton6.Enabled = True
End If
End If
End If
End SubEl problema es las variables FechaInicio y FechaFinal, cuando cierro el archivo de excel y lo abro de nuevo me marca el error "Run-time '91':

Entonces modifico las variables cambiándolas a Range en el tipo de variable y la codificación que lleva:
Sub pruebas()
Dim Fecha As Date, Rango As Range, RangoF As Range, FechaFinal As Range, FechaInicio As Range, RangoC As Range
Set Rango = Sheet2.ListObjects("Table1").ListColumns(2).Range
TurnoX = Sheet1.Cells(Rows.Count, "G").End(xlUp).Value
Fecha = Date - 1 'Format(Date, "MMDDYYYY")
Set FechaInicio = Rango.Find(Fecha, LookIn:=xlValues, searchdirection:=xlNext)
Set FechaFinal = Rango.Find(Fecha, LookIn:=xlValues, searchdirection:=xlPrevious)asi funciona, pero no me da el resultado que yo espero, una vez hecho esto regreso el código como lo puse al inicio y funciona sin problema -.- no entiendo el error o porque pasa.
1 Respuesta
Respuesta de Dante Amor
4