Я пытаюсь создать MACRO для vlook-up.XLS VBA VlookUp с использованием RC от статического до динамического
У меня 2 листа. Мне удалось заставить его работать с VlookUp hardcoded.
Могу ли я сделать его динамичным?
Вот мой код:
Option Explicit
Sub VlookUp4()
Dim NrColsOld, NrColsNew As Integer 'Numarul de celule in primul rand, incepand cu A1 sheet "old" si "new"
Dim FoundOld, FoundNew As Range
Dim LROld, LRNew As Long
Dim Cauta As Variant
'Cauta = InputBox("Filtru dupa ce coloana?")
Sheets("old").Select
With ActiveSheet
NrColsOld = .Cells(1, .Columns.Count).End(xlToLeft).Column ' Calculeaza care e ultima coloana din sheet
End With
'Set Found = Rows(1).Find(What:=Cauta, LookIn:=xlValues, lookat:=xlWhole)
Set FoundOld = Rows(1).Find(What:="Numar", LookIn:=xlValues, lookat:=xlWhole)
If FoundOld Is Nothing Then Exit Sub
LROld = Cells(Rows.Count, FoundOld.Column).End(xlUp).Row
ActiveSheet.Range(Cells(1, 1), Cells(LROld, FoundOld.Column + NrColsOld)).AutoFilter
Worksheets("old").Range(Cells(1, 1), Cells(LROld, FoundOld.Column + NrColsOld)).Columns.AutoFit
Range(Cells(1, 1), Cells(LROld, NrColsOld)).Select 'selecteaza celulele ce contin valori
ActiveWorkbook.Worksheets("old").AutoFilter.Sort.SortFields.Add Key:=Range(Cells(1, FoundOld.Column), Cells(1, FoundOld.Column)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
'Range(Cells(1, Found.Column), Cells(1, Found.Column)).Select 'selecteaza doar celula cu numele celulei dupa care facem ordonarea
With ActiveWorkbook.Worksheets("old").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("new").Select
With ActiveSheet
NrColsNew = .Cells(1, .Columns.Count).End(xlToLeft).Column ' Calculeaza care e ultima coloana din sheet
End With
'Set Found = Rows(1).Find(What:=Cauta, LookIn:=xlValues, lookat:=xlWhole)
Set FoundNew = Rows(1).Find(What:="Numar", LookIn:=xlValues, lookat:=xlWhole)
If FoundNew Is Nothing Then Exit Sub
LRNew = Cells(Rows.Count, FoundNew.Column).End(xlUp).Row
ActiveSheet.Range(Cells(1, 1), Cells(LRNew, FoundNew.Column + NrColsNew)).AutoFilter
Worksheets("new").Range(Cells(1, 1), Cells(LRNew, FoundNew.Column + NrColsNew)).Columns.AutoFit
Range(Cells(1, 1), Cells(LRNew, NrColsNew + 1)).Select 'selecteaza celulele ce contin valori + 1 se adauga pentru ca se insereaza o coloana
ActiveWorkbook.Worksheets("new").AutoFilter.Sort.SortFields.Add Key:=Range(Cells(1, FoundNew.Column), Cells(1, FoundNew.Column)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
'Range(Cells(1, Found.Column), Cells(1, Found.Column)).Select 'selecteaza doar celula cu numele celulei dupa care facem ordonarea
With ActiveWorkbook.Worksheets("new").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
FoundNew.Offset(, 1).EntireColumn.Insert
Cells(1, FoundNew.Column + 1).Value = "New vs Old"
Dim unu, doi, trei As Integer
unu = 1
doi = 2
trei = 3
Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).FormulaR1C1 = "=VLOOKUP(RC[-1],old!C[-3]:C[-2],2,0)"
'Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).FormulaR1C1 = "=VLOOKUP(RC[-unu],old!C[-trei]:C[-doi],2,0)"
' Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).Select
' Selection.Copy
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' Selection.Replace What:="#N/A", Replacement:="Intrari Noi", lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
' ActiveSheet.Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).AutoFilter field:=5, Criteria1:="Intrari Noi"
End Sub
Сначала я хочу, чтобы преобразовать
Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).FormulaR1C1 = "=VLOOKUP(RC[-1],old!C[-3]:C[-2],2,0)"
в
Dim unu, doi, trei As Integer
unu = 1
doi = 2
trei = 3
Range(Cells(2, FoundNew.Column + 1), Cells(LRNew, FoundNew.Column + 1)).FormulaR1C1 = "=VLOOKUP(RC[-unu],old!C[-trei]:C[-doi],2,0)"
Может кто-нибудь дать мне подсказку, пожалуйста.
Спасибо.
спасибо. работает отлично. –
Для тех из вас, кому может понадобиться нечто подобное, вот полный код: –