RE:Cargar archivo txt (Macro)
Después de mucho probar diferentes métodos
para encontrar la 'Bendita primer celda vacía',
llegué a la conclusión de que los métodos
más lentos son los que utilizan bucles
Do Loop / While
o el método Find / FindNext
Después le siguen (en orden de velocidad) los bucles
For Each
Next
siendo estos bastante aceptables y los más
rápidos para procesar muchos datos en diversas situaciones.
Pero el método más veloz -por excelencia- para encontrar
la primer celda vacía -sin importar la
cantidad de datos que haya que procesar-
es el siguiente:
Sub test()
Dim rng As Range
Set rng = UltimaCeldaConDato("a", 1)
If Not rng Is Nothing Then
If rng.Row <> Cells.Rows.Count Then
MsgBox "Primer celda vacía: " & rng.Offset(1, 0).Address
Else
MsgBox "No hay celdas vacías después de la última celda con datos!"
End If
Else
MsgBox "Primer celda vacía: $A$1"
End If
Set rng = Nothing
End Sub
Private Function UltimaCeldaConDato(ByVal sColumna As String, ByVal lFilaPivot As Long) As Range
Dim lRow&, iCol%
Set UltimaCeldaConDato = Nothing
If lFilaPivot >= Cells.Rows.Count Or lFilaPivot <= 0 Then Exit Function
iCol = cLetterToNumber(sColumna)
If iCol <= 0 Or iCol > Cells.Columns.Count Then Exit Function
If Range(sColumna & Cells.Rows.Count).Value <> "" Then
lRow = Cells.Rows.Count
Else
lRow = Range(sColumna & Cells.Rows.Count).End(xlUp).Row
End If
If Not (lRow = 1 And IsEmpty(Cells(lRow, iCol).Value)) Then
Set UltimaCeldaConDato = Range(Cells(lRow, iCol).Address)
End If
End Function
Private Function cLetterToNumber(ByVal sLetter As String) As Integer
Dim l%, n%
cLetterToNumber = 0
l = Len(Trim(sLetter))
If l = 0 Or l > 2 Then Exit Function
If l = 1 Then
n = Asc(LCase(sLetter)) - 96
If n > 0 And n <= 26 Then cLetterToNumber = n
Exit Function
Else
cLetterToNumber = cConvLtrToNum(UCase(sLetter))
End If
End Function
Private Function cConvLtrToNum(ByVal LtrIn As String) As Integer
Dim sChar$, iNum%, i%, sString$, l%
Dim iArray() As Integer, iPower%
sChar = "": iNum = 0: cConvLtrToNum = 0
l = Len(LtrIn)
For i = 1 To l
sString = ""
sChar = Mid(LtrIn, i, 1)
If Asc(sChar) < 65 Or Asc(sChar) > 90 Then Exit Function
ReDim Preserve iArray(i)
iArray(i) = Asc(sChar) - 64
Next
iPower = UBound(iArray()) - 1
l = UBound(iArray())
For i = 1 To l
iNum = iNum + (iArray(i) * (26 ^ iPower))
iPower = iPower - 1
Next
If iNum > Cells.Columns.Count Then
cConvLtrToNum = 0
Else: cConvLtrToNum = iNum
End If
End Function
Saludos desde Baires, JuanC