I am a newbie to VBA, and I try to Insert a VLOOKUP
function.
I get the data from a webpage and after some autofiltering and auto-deleting, data shrinks to a smaller size. But since it is a dynamic data, the last row always changes.
My data is like this:
A B C
FleetType MSN Registration
I have the registration ( C
) filled up, and want to find the fleet type which is listed in a different sheet.
I tried inserting a formula by using FormulaR1C1
, but eventually get a 1004 message. I also tried to use Application.VLookup
, also get the 1004 message.
Since I could not make this work, as I know the first two letters of the registration gives a clue about the fleet type, I have setted up a nested IF
function and tried to insert it; without luck also.
This is the sub
Im trying to make it run (I have not deleted the ones that I tried):
Sub formuller()
Worksheets("Data").Select
Worksheets("Data").Activate
'range("A1:A600").Formula = "=IF(LEFT(D2;2)="JK";"Boeing";IF(LEFT(D2;2)="JG";"Boeing";IF(LEFT(D2;2)="JF";"Boeing";IF(LEFT(D2;2)="JH";"Boeing";IF(LEFT(D2;2)="JV";"Boeing";IF(LEFT(D2;2)="JJ";"Boeing";IF(LEFT(D2;2)="JY";"Boeing";IF(LEFT(D2;2)="LJ";"Boeing";"Airbus"))))))))"
On Error Resume Next
Range("A1:A2").FormulaR1C1 = "=IF(LEFT(RC[3];2)=""JK"";""Boeing"";IF(LEFT(RC[3];2)=""JG"";""Boeing"";IF(LEFT(RC[3];2)=""JF"";""Boeing"";IF(LEFT(RC[3];2)=""JH"";""Boeing"";IF(LEFT(RC[3];2)=""JV"";""Boeing"";IF(LEFT(RC[3];2)=""JJ"";""Boeing"";IF(LEFT(RC[3];2)=""JY"";""Boeing"";IF(LEFT(RC[3];2)=""LJ"";""Boeing"";""Airbus""))))))))"
On Error GoTo 0
'Worksheets("Data").Range("A1:A600").FormulaR1C1 = "=VLOOKUP(RC[3];Filo!R1C1:R309C3;3;FALSE)"
Is there any way around this? All I am asking is for a way around just using the good old VLookup
in a VBA project.
You can use WorksheetFunction.VLookup
for good old VLookup
. But alternativly you can use this macro for your porpuse. Explanation is in comments.
Option Explicit
Sub FindFleetType()
Const Boeing As String = "Boeing"
Const Airbus As String = "Airbus"
Dim MySheet As Worksheet
Dim MyRange, c As Range
Dim MyInt, LastRow As Integer
Dim MyStr, MyArray(7) As String
'filling string array
MyArray(0) = "JK"
MyArray(1) = "JG"
MyArray(2) = "JF"
MyArray(3) = "JH"
MyArray(4) = "JV"
MyArray(5) = "JJ"
MyArray(6) = "JY"
MyArray(7) = "LJ"
'defining worksheet
Set MySheet = ThisWorkbook.Worksheets("Data")
'finding lastrow has data on columnn C
'so you don't have to change the range manually every time
With MySheet
LastRow = .Range("C" & .Rows.Count).End(xlUp).Row
End With
'defining range to be filled
Set MyRange = ThisWorkbook.ActiveSheet.Range("A2:A" & LastRow)
'iterating each cell on defined range
For Each c In MyRange
MyStr = Left(c.Offset(0, 2).Value2, 2) 'get first two letters of "Registration" column C
'control if your array contains your string
If ContainString(MyStr, MyArray) Then
c.Value2 = Boeing
Else
c.Value2 = Airbus
End If
Next
End Sub
Private Function ContainString(ByVal StringToBeFound As String, StringArray As Variant) As Boolean
ContainString = (UBound(Filter(StringArray, StringToBeFound)) > -1)
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.