Amandina
asked on
Problem with Vlookup in VBA
Hi,
I am trying to use Vlookup in a macro but can't get it to Work.
I have a sheet called "Pivot" and a sheet called "Data2".
I build a new sheet called "Raabalance" with data from both sheets with this code:
Sub Opdaterdata()
Dim i_rowidx1, i_rowidx2, i_rowidx3 As Integer
Dim rngstart As Range
Dim primo As Currency
Dim k_nr As String
'On Error GoTo Fejl
Set rngstart = Worksheets("Pivot").Range( "A1")
i_data1_idx = rngstart.CurrentRegion.Row s.Count
On Error Resume Next
i_vareliste_idx = 2
For i = 2 To i_data1_idx
k_nr = Worksheets("Pivot").Cells( i, 1)
primo = 0
' primo = [VLOOKUP(k_nr, Data2!A1:X200, 2, FALSE)]
' primo = Application.VLookup(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
' primo = Worksheet.VLookup(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
' primo = Application.WorksheetFunct ion.VLooku p(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
' primo = Application.Worksheet.VLoo kup(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
' primo = Application.Worksheet.VLoo kup(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
'primo = Application.Worksheet.Func tion.VLook up(k_nr, Tabel_Forespørgsel_fra_EGD TA1, 2, 0)
primo = Application.WorksheetFunct ion.VLooku p(k_nr, "Data2!a1:x400", 2, 0)
'Kontonr
Worksheets("Raabalance").C ells(i_var eliste_idx , 1).Value = Worksheets("Pivot").Cells( i, 1)
'Saldi januar-december
Worksheets("Raabalance").C ells(i_var eliste_idx , 2).Value = Worksheets("Pivot").Cells( i, 2) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 3).Value = Worksheets("Pivot").Cells( i, 3) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 4).Value = Worksheets("Pivot").Cells( i, 4) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 5).Value = Worksheets("Pivot").Cells( i, 5) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 6).Value = Worksheets("Pivot").Cells( i, 6) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 7).Value = Worksheets("Pivot").Cells( i, 7) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 8).Value = Worksheets("Pivot").Cells( i, 8) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 9).Value = Worksheets("Pivot").Cells( i, 9) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 10).Value = Worksheets("Pivot").Cells( i, 10) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 11).Value = Worksheets("Pivot").Cells( i, 11) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 12).Value = Worksheets("Pivot").Cells( i, 12) + primo
Worksheets("Raabalance").C ells(i_var eliste_idx , 13).Value = Worksheets("Pivot").Cells( i, 13) + primo
i_vareliste_idx = i_vareliste_idx + 1
Next i
End Sub
The Vlookup for the value "Primo" is not working.
Cells from sheet "Pivot":
71112 0 0 -514,66 0 0 -514,66 0 0 -514,66 0 0 0
Cells from sheet "Data2"
71112 -2058,64
Please help...
Regards Bente
I am trying to use Vlookup in a macro but can't get it to Work.
I have a sheet called "Pivot" and a sheet called "Data2".
I build a new sheet called "Raabalance" with data from both sheets with this code:
Sub Opdaterdata()
Dim i_rowidx1, i_rowidx2, i_rowidx3 As Integer
Dim rngstart As Range
Dim primo As Currency
Dim k_nr As String
'On Error GoTo Fejl
Set rngstart = Worksheets("Pivot").Range(
i_data1_idx = rngstart.CurrentRegion.Row
On Error Resume Next
i_vareliste_idx = 2
For i = 2 To i_data1_idx
k_nr = Worksheets("Pivot").Cells(
primo = 0
' primo = [VLOOKUP(k_nr, Data2!A1:X200, 2, FALSE)]
' primo = Application.VLookup(k_nr, Tabel_Forespørgsel_fra_EGD
' primo = Worksheet.VLookup(k_nr, Tabel_Forespørgsel_fra_EGD
' primo = Application.WorksheetFunct
' primo = Application.Worksheet.VLoo
' primo = Application.Worksheet.VLoo
'primo = Application.Worksheet.Func
primo = Application.WorksheetFunct
'Kontonr
Worksheets("Raabalance").C
'Saldi januar-december
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
Worksheets("Raabalance").C
i_vareliste_idx = i_vareliste_idx + 1
Next i
End Sub
The Vlookup for the value "Primo" is not working.
Cells from sheet "Pivot":
71112 0 0 -514,66 0 0 -514,66 0 0 -514,66 0 0 0
Cells from sheet "Data2"
71112 -2058,64
Please help...
Regards Bente
How about a sample file for testing. There might even be a problem with the data.
Try
primo = Application.WorksheetFunct ion.VLooku p(k_nr, range("Data2!a1:x400"), 2, 0)
primo = Application.WorksheetFunct
ASKER
Hi Saqib,
I tried your first comment, did not Work.
I have created a sample file with just a little data as it is confidental.
Hope you can help.
R-balance-test.xlsm
I tried your first comment, did not Work.
I have created a sample file with just a little data as it is confidental.
Hope you can help.
R-balance-test.xlsm
Ok then try
primo = Application.WorksheetFunct ion.VLooku p(Workshee ts("Pivot" ).Cells(i, 1), Range("Data2!a1:x400"), 2, 0)
primo = Application.WorksheetFunct
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you are developing code do not use error handlers until the code actually works. If you do then possible error alerts that may help will be stopped.
Instead of VLOOKUP in Excel look at the .Find method in VBA
Instead of VLOOKUP in Excel look at the .Find method in VBA
ASKER
Thanks!
Does that mean that the second formula I gave did not work?
ASKER
No, it did not Work. The problem was the difference between string and numbers. But thanks