Search Left

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi - I'm using this code as a vlookup. Is there a way to alter the code so that i'm only looking up the first 3 digits of the value in Column 6 on Sheet6?

Code:
Dim LastRow, LR1, LR2, LR3, i As LongDim Searchfor, j, inarr As Variant


Set wsRS = Sheet6 
Set wsDT = Sheet2 


With wsDT
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    inarr = .Range(.Cells(2, 1), .Cells(LastRow, 2))
End With


With wsRS
    LR1 = .Cells(Rows.Count, "A").End(xlUp).Row
    searcharr = .Range(.Cells(2, 6), .Cells(LR1, 6))
    outarr = .Range(.Cells(2, 15), .Cells(LR1, 15))
End With


On Error Resume Next
For i = 1 To LR1
For j = 1 To LastRow
Searchfor = searcharr(i, 1)


If inarr(j, 1) = Searchfor Then
    For kk = 2 To 2
        outarr(i, kk - 1) = inarr(j, kk)
    Next kk
    Exit For
End If


Next j
Next i


With wsRS
    .Range(.Cells(2, 15), .Cells(LR1, 15)) = outarr
        .Range("D7:D31").HorizontalAlignment = xlCenter
End With
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Do you mean that sheet2 col A has 3digit values & you want to find them in col 6 of sheet1?
 
Upvote 0
yes, sheet2 has 3 digit values in column A. So i want to lookup the 1st 3 digits from column F on sheet6. thanks!
 
Upvote 0
Ok, try
Code:
Sub helpexcel()
   Dim Ary As Variant, Nary As Variant, Oary As Variant
   Dim i As Long, UsdRws As Long

   With Sheet2
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp).Offset(, 1)).Value2
   End With
   With Sheet6
      UsdRws = .Range("A" & Rows.Count).End(xlUp).Row
      Nary = .Range("F2:F" & UsdRws).Value2
      Oary = .Range("O2:O" & UsdRws).Value2
   End With

   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For i = 1 To UBound(Ary)
         .Item(Ary(i, 1)) = Ary(i, 2)
      Next i
      For i = 1 To UBound(Nary)
         Oary(i, 1) = .Item(Left(Nary(i, 1), 3))
      Next i
   End With
   With Sheet6
      .Range("O2:O" & UsdRws).Value = Oary
      .Range("D7:D31").HorizontalAlignment = xlCenter
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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