Vale, supongo que quieres que se corra la macro al escribir y dar ENTER o pasarte a la siguiente celda.
Cambie un poco el código rápido, pruébalo así y si te parece extraño o incomodo me dejas saber y mañana te hago uno más productivo que ya es tarde acá.
Substituye el código que te di antes por este y otra vez, ajusta la ruta.
Sub CreateReference(DNInum As String, DataRow As Integer)
Application.ScreenUpdating = False
Dim CrrtWS As Worksheet
Set CrrtWS = ActiveSheet
Dim TempDB As Worksheet
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "TempDB"
Set TempDB = ActiveSheet
With TempDB.QueryTables.Add(Connection:="TEXT;C:\Users\andym\ruc.txt", _
Destination:=Range("$A$1"))
.Name = "ruc"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
CrrtWS.Activate
Call FindDNI(DNInum, DataRow)
Application.DisplayAlerts = False
Sheets("TempDB").Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub FindDNI(DNInum As String, DataRow As Integer)
Dim uF As Long
Dim DNI As String
Dim Rng As Range
DNI = DNInum
If Trim(DNI) <> "" Then
With Sheets("TempDB").Range("A:A")
Set Rng = .Find(What:=DNI, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Cells(DataRow, 2).Value = Rng.Offset(0, 1).Value
Cells(DataRow, 3).Value = Rng.Offset(0, 2).Value
Cells(DataRow, 4).Value = Rng.Offset(0, 3).Value
Else
MsgBox "No se encontro ese DNI", vbExclamation, "No existe..."
End If
End With
End If
End Sub
Ahora vas a tener que agregar un codigo nuevo, en el modulo de la hoja. Das click derecho a la pestaña de la hoja y le das click en "Ver codigo" y pegas lo siguiente:
Option Explicit
Public OldCell As String
Public NewCell As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect As Range
Set isect = Intersect(Target, Me.Range("$A:$A"))
If isect Is Nothing Then Exit Sub
If NewCell = "" Then NewCell = ActiveCell.Address
OldCell = NewCell
NewCell = ActiveCell.Address
Call CreateReference(Range(OldCell).Value, Range(OldCell).Row)
End Sub
Eso va a ejecutar la macro cada vez que escribas un DNI en la columna A y pases a la siguiente celda.
Repito, si te parece incomodo o raro la manera de trabajarlo, me avisas y mañana lo arreglamos que esto lo hice a lo loco.
Andy M