Uso de buscarx en VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-2296750%22%20slang%3D%22es-ES%22%3EUsing%20searchx%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2296750%22%20slang%3D%22es-ES%22%3E%3CP%3EGood%20morning.%3C%2FP%3E%3CP%3EI'm%20trying%20to%20automate%20a%20report%20that%20has%20to%20search%20for%20values%20from%20one%20sheet%20and%20paste%20them%20into%20another%20sheet%20in%20another%20file.%3C%2FP%3E%3CP%3EI%20created%20a%20macro%20that%20traverses%20a%20dynamic%20range%20that%20determines%20the%20only%20one%20function%20of%20cells%20with%20values%20in%20a%20column.%3C%2FP%3E%3CP%3EThe%20problem%20is%2C%20I%20want%20to%20bring%20all%20the%20data%20in%20the%20row.%20In%20a%20normal%20sheet%2C%20if%20I%20searchX%20I%20put%20as%20a%20result%20of%20the%20search%20the%20full%20range%20of%20the%20array%2C%20this%20returns%20the%20whole%20row%2C%20but%20in%20VBA%20only%20returns%20data%20from%20the%20first%20column.%3C%2FP%3E%3CP%3EYou%20know%20why%3F%3C%2FP%3E%3CP%3Ecode%3A%3C%2FP%3E%3CP%3EDim%20j%2C%20k%2C%20l%2C%20ini5%20As%20Range%20%3CBR%20%2F%3E%20Range(%22b4%22).%20Select%20%3CBR%20%2F%3E%20dimension3%20-%20Range(Selection%2C%20Selection.End(xlDown)).%20Count%20-%201%20%3CBR%20%2F%3E%20Selection.Offset(0%2C%201).%20Select%20%3CBR%20%2F%3E%20Set%20ini5%20%3F%20Range(%22b4%22)%20%3CBR%20%2F%3E%20Set%20j%20%3F%20Range(ini5%2C%20ini5.%20Offset(dimension3%2C%200))%20Set%20l%20'%20%3CBR%20%2F%3E%20Range(ActiveCell%2C%20ActiveCell.Offset(dimension3%2C%200))%20%3CBR%20%2F%3E%20For%20Each%20k%20In%20j%20%3CBR%20%2F%3E%20l.Value%20%3F%20(Application.XLookup(j%2C%20Workbooks(first).%20Worksheets(%22MONTHLY%20Report%22).%20Range(%22b4%3Ab30%22)%2C%20Workbooks(first).%20Worksheets(%22MONTHLY%20Report%22).%20Range(%22c4%3An30%22)%2C%200%2C%200%2C%201))%20%3CBR%20%2F%3E%20Next%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%22First%22%20is%20the%20variable%20I%20defined%20before%20with%20the%20name%20of%20the%20data%20source%20file.%20(I%20do%20it%20elsewhere%20in%20the%20code).%3C%2FP%3E%3CP%3E%22B4%22%20is%20the%20first%20cell%20in%20the%20column%20that%20determines%20the%20variable%20range%20and%20where%20the%20references%20to%20search%20for%20X%20are.%3C%2FP%3E%3CP%3E%22Dimension3%22%20is%20the%20number%20of%20rows%20in%20the%20range.%3C%2FP%3E%3CP%3E%22l%22%20is%20the%20rank%20where%20I%20want%20to%20put%20the%20search%20results.%3C%2FP%3E%3CP%3E%22k%22%20is%20each%20cell%20in%20the%20%22j%22%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2296750%22%20slang%3D%22es-ES%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Visitor

Buenos días.

Estoy intentando automatizar un informe que tiene que buscar unos valores de una hoja y pegarlos en otra hoja de otro archivo.

He creado una macro que recorre un rango dinámico que se determina el solo en función de las celdas con valores de una columna determinada. 

El problema es que me quiero traer toda las datos de la fila, para ello quería usar BuscarX.

Si en una hoja normal de Excel365 al usar  BuscarX le pongo como resultado de la del búsqueda el rango completo de la matriz, este me devuelve toda la fila, pero en VBA solo me devuelve datos de la primera columna. 

¿sabeis por que?

Código: 

Dim j, k, l, ini5 As Range
Range("b4").Select
dimension3 = Range(Selection, Selection.End(xlDown)).Count - 1
Selection.Offset(0, 1).Select
Set ini5 = Range("b4")
Set j = Range(ini5, ini5.Offset(dimension3, 0))
Set l = Range(ActiveCell, ActiveCell.Offset(dimension3, 0))
For Each k In j
l.Value = (Application.XLookup(j, Workbooks(primero).Worksheets("Informe MENSUAL").Range("b4:b30"), Workbooks(primero).Worksheets("Informe MENSUAL").Range("c4:n30"), 0, 0, 1))
Next

 

"Primero" es la variable que he definido antes con el nombre del archivo fuente de datos (lo hago en otra parte del código).

"B4" es la primera celda de la columna que determina el rango variable y donde están las referencias que debe buscar BuscarX.

"Dimension3" es el nº de filas del rango.

"l" es el rango donde quiero poner los resultado de la búsqueda.

"k" es cada celda del rango "j".

 

0 Replies