Error on my small VBA when condition is met

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hello,

Here is my code :

Code:
Sub VlookupAmeliorer()
'
' Macro2 Macro
'


    Dim i As Integer
    Dim LastRowL As Integer
    LastRowL = Sheets("PlanEX-Origine").Cells(Rows.Count, 12).End(xlUp).Row
    For i = 2 To LastRowL
    
    If Len(Cells(23, i)) < 255 Then
    Range("W" & i).FormulaR1C1 = "=+IF(RC[-4]=1,""ok"",VLOOKUP(RC[-1],C[16]:C[24],2,0))"
    Else
    Range("W" & i).FormulaR1C1 = "test"
        End If
    Next i


End Sub


After a short time, it comes with an error 104 Object (or application) defined error.
I am clueless... please can you guide me to the correct solution
Basically i would like the macro to copy a vlookup in every cell untill the last cell if and adjacent cell has less than 255 characters (otherwise VLOOKUP bring errors). If it is above 255 then writes "test".

Thats all. Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This line
Code:
If Len(Cells(23, i)) < 255 Then
should be
Code:
If Len(Cells(i,24)) < 255 Then
Asuming that "adjacent cell" means col X
 
Upvote 0
This line
Code:
If Len(Cells(23, i)) < 255 Then
should be
Code:
If Len(Cells(i,24)) < 255 Then
Asuming that "adjacent cell" means col X


Thanks Fluff. It solved the problem. But I discovered another one...
I added a formula if the cell length is above 255.

Code:
Sub VlookupAmeliorer()
    Dim i As Integer
    Dim LastRowL As Integer
    LastRowL = Sheets("PlanEX-Origine").Cells(Rows.Count, 12).End(xlUp).Row
    For i = 2 To LastRowL
    
    If Len(Cells(i, 24)) < 255 Then
    Range("W" & i).FormulaR1C1 = "=+IF(RC[-4]=1,""ok"",VLOOKUP(RC[-1],C[16]:C[24],2,0))"
    Else
    Range("W" & i).FormulaR1C1 = "=+IF(RC[-4]=1,""ok"",MyVlookup(RC[-1],C[16]:C[24],2))"
        End If
    Next i
End Sub


Code:
Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
'It's an exact result only. The function loops through the first column in the lookup range and compares it to the lookup value.
'If it is the same, then it returns the value of the relevant cell to the right.


Dim cl As Range
For Each cl In c.Columns(1).Cells
    If UCase(Lval) = UCase(cl) Then
        MyVlookup = cl.Offset(, oset - 1)
        Exit Function
    End If
    Next
End Function

But it does not work....

My ultimate goal is to use the Vlookup if the character length is above 255 and the formula MyVookup otherwise.
I have to find a solution because of this stupid 255 max characteres for the vlookup.

Thanks
 
Upvote 0
What if you change this
Code:
If Len(Cells(i, [COLOR=#ff0000]24[/COLOR])) < 255 Then
to 22 not 24
 
Upvote 0
What if you change this
Code:
If Len(Cells(i, [COLOR=#ff0000]24[/COLOR])) < 255 Then
to 22 not 24



Thanks for your help an time Fluff.

Here is my solution :

I use an index/Match if character is less than 255 and otherwise the formule in MyVlookup.
=IF(S2=1,"ok",+IF(LEN(V2)>255,MyVlookup(V2,TabPassage,2),INDEX(TabProbOK,MATCH(V2,TabIDPassage,0))))

and I use it with the VBA seen above with the changes you told me to do.

Thanks again.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,717
Members
449,050
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top