Migración

'Modulo Principal
Public origen As String
Public destino As String
Public migrar As String

Sub Gral()

migrar = ActiveWorkbook.Name
UserForm1.Show

End Sub

'Formulario1
Private Sub CommandButton1_Click()
origen = ActiveWorkbook.Name
Unload UserForm1
UserForm2.Show
End Sub

'Formulario2
Private Sub CommandButton1_Click()

destino = ActiveWorkbook.Name
Windows(origen).Activate
Sheets("Base de datos").Activate
Range("A3").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
Windows(destino).Activate
Sheets("Base de datos").Activate
Range("A3").Select
ActiveSheet.Paste
Application.CutCopyMode = False

Windows(origen).Activate
Sheets("Pendientes").Visible = True
Sheets("Pendientes").Activate
Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
Windows(destino).Activate
Sheets("Pendientes").Visible = True
Sheets("Pendientes").Activate
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Pendientes").Visible = xlVeryHidden

Windows(origen).Activate
Sheets("Rechazados").Visible = True
Sheets("Rechazados").Activate
Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
Windows(destino).Activate
Sheets("Rechazados").Visible = True
Sheets("Rechazados").Activate
Range("A2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
    Columns("BK:BK").Select
    Selection.Insert Shift:=xlToRight
    Range("BK1").Select
    Selection.Delete Shift:=xlToLeft
Sheets("Rechazados").Visible = xlVeryHidden

Unload UserForm2
ActiveWorkbook.Save
MsgBox "Gracias"
Windows(migrar).Activate
ActiveWorkbook.Close

End Sub

------------------------

Sub prueba1()
Dim objetivo As String
Dim rng As Range
objetivo = Range("A1").Value
Set rng = Sheets("Hoja2").Range("A1:a100")
Range("A2").Activate

ActiveCell.Value = Compara(objetivo, Range("E1").Value)
ActiveCell.Offset(1, 0).Activate

End Sub
Sub prueba()
Dim objetivo As String
Dim rng As Range

Set rng = Sheets("Hoja2").Range("A1:a10891")
Set rng1 = Sheets("Hoja1").Range("A1:a107")
Range("A1").Activate
For i = 1 To 3
objetivo = ActiveCell.Value
a = 1
    For Each Item In rng
    If Compara(objetivo, Item) > 0.85 Then
    ActiveCell.Offset(0, a).Value = Item
    a = a + 1
    End If
    Next
ActiveCell.Offset(1, 0).Activate
Next i
End Sub

Function Compara(ByVal sCad1 As String, ByVal sCad2 As String) As Double
'Preparación previa
If Len(sCad1) > Len(sCad2) Then
Dim sAux As String
sAux = sCad1
sCad1 = sCad2
sCad2 = sAux
End If
Dim lLong As Long
lLong = Len(sCad2)
If lLong = 0 Then
Compara = 1
Exit Function
End If
sCad1 = sCad1 + Space(lLong - Len(sCad1))
'Recorremos las cadenas
Dim sChar As String * 1
Dim i As Integer, j As Integer, iPos As Integer, iDist As Integer, iTotDist As Integer
For i = 1 To lLong
sChar = Mid(sCad1, i, 1)
iDist = lLong
For j = 1 To lLong
'Si coinciden 2 caracteres vemos la distancia y nos quedamos con la menor
If sChar = Mid(sCad2, j, 1) And Abs(j - i) < iDist Then
iDist = Abs(j - i)
iPos = j
End If
Next
'Eliminamos el caracter para que no se "multicompare"
'Se cambia por "~", caracter que no debe estar en las cadenas que se comparan
If iDist < lLong Then sCad2 = Left(sCad2, iPos - 1) & "~" & Mid(sCad2, iPos + 1)
iTotDist = iTotDist + iDist
Next
'La fórmula
Compara = 1 - iTotDist / (lLong ^ 2)
End Function
Public Function COMPARA_APN(ByVal CAD1 As String, ByVal CAD2 As String) As Double
On Error GoTo ErrorFunction
'VALOR INICIAL
COMPARA_APN = 0
'EN CAD1 EL DE MAYOR LARGO, ORDENAR
If Len(CAD2) > Len(CAD1) Then
AUX = CAD1
CAD1 = CAD2
CAD2 = AUX
End If
DIF = Len(CAD1) - Len(CAD2)
For j = 0 To DIF
For POS = 0 To Len(CAD2) - 1
Q = 0
AUX = Left(CAD2, POS) & Space(j) & Right(CAD2, Len(CAD2) - POS) & Space(DIF - j)
'1 COMPARAMOS LETRA A LETRA
For i = 1 To Len(CAD1)
If Mid(CAD1, i, 1) = Mid(AUX, i, 1) Then Q = Q + 1
Next
If Q / Len(CAD1) > COMPARA_APN Then COMPARA_APN = Q / Len(CAD1)
Next
Next
'exit sub
ErrorFunction:
MsgBox Err.Description, vbCritical
Err.Clear
End Function


No hay comentarios:

Publicar un comentario