Lookup in other sheet VBA

billmark

Board Regular
Joined
Dec 11, 2011
Messages
59
Hi
I have the codes to perform the following:
1. I have data in sheet1
2. I have a range of numbers in sheet3, from A1 to A10
3. If the data in column1 of sheet1 matches to that in A1 to A10 in sheet3, then add "S" to the end of the data in column27 and then make column26 the same as column27.

Please advise how to fix as it doesn't work

QUOTE
Code:
Sub sbNameRange()
    Dim WB As Workbook
    Dim dataSH As Worksheet, lookupSHsub As Worksheet
    Dim dataRng As Range, lookupRngsub As Range
    Dim rCell As Range
    Dim Res As Variant
    Dim LRow As Long
    Dim i As Integer
    


    Const sDataSheet As String = "Sheet1"
    Const sLookupSheet3 As String = "Sheet3"
    Const sLookupRange3 As String = "A1:A10"
    
       
   
    
    Set WB = ThisWorkbook


    With WB
        Set dataSH = .Sheets(sDataSheet)
        Set lookupSHsub = .Sheets(sLookupSheet3)
    End With


    Set lookupRngsub = lookupSHsub.Range(sLookupRange3)




    With dataSH
        LRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set dataRng = .Range("A1:A" & LRow)
    End With


    
   i = 2
  For Each rCell In dataRng.Cells
        With rCell
            Res = Application.Match(.Value, lookupRngsub, 0)
            If Not IsError(Res) _
                            And .Offset(0, 0).Value = sLookupRange3 Then
                Cells(i, 27) = Cells(i, 27) & " " & "S"
                Cells(i, 26) = Cells(i, 27)
            End If
            
            
            
        End With
    Next rCell
    
End Sub
UNQUOTE
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi billmark,

Try this:

Code:
Option Explicit
Sub Macro1()

    Dim rngMyCell As Range
    Dim lngLastRow As Long
    Dim varResult As Variant
    
    Application.ScreenUpdating = False
    
    lngLastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
    For Each rngMyCell In Sheets("Sheet1").Range("A1:A" & lngLastRow)
        If Len(rngMyCell) > 0 Then
            varResult = Evaluate("VLOOKUP(""" & rngMyCell & """,Sheet3!$A$1:$A$10,1,0)")
            If IsError(varResult) = False Then
                With Sheets("Sheet1")
                    .Range("AA" & rngMyCell.Row).Value = .Range("AA" & rngMyCell.Row).Value & "S"
                    .Range("Z" & rngMyCell.Row).Value = .Range("AA" & rngMyCell.Row).Value
                End With
            End If
        End If
    Next rngMyCell
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi Robert
Thanks

However, it doesn't work. When I placed the cursor on varResult, it showed varResult=Error 2042.

Please help.

Regards
 
Upvote 0
Please advise how to fix as it doesn't work
If we knew what "doesn't work" meant then we may be able to help. ;)
 
Upvote 0
Hi
No change in columns 26 and 27 after running the code, ie "S" is not added to end of the data in column27 and thus column26 the same as column27
 
Upvote 0
How about
Code:
For Each rCell In DataRng.Cells
   With rCell
      Res = Application.Match(.Value, lookupRngsub, 0)
      If Not IsError(Res) And .Value = sLookupRange3 Then
         .Offset(, 26).Value = .Offset(, 26).Value & " S"
         .Offset(, 25).Value = .Offset(, 26).Value
      End If
   End With
Next rCell
 
Upvote 0
"S" is still not added. But I remove this condition "And .Value = sLookupRange3", it works.

Anyway, thanks
 
Upvote 0
However, it doesn't work.

That's odd - it worked for me :confused: I agree with Fluff - we can't offer a solution if we don't know what's broken.

When I placed the cursor on varResult, it showed varResult=Error 2042.

That's expected. It just means there was no match from the VLOOKUP formula in the code so the code goes to the next cell. If there are no exact matches in column A of Sheet1 to cells A1:A10 of Sheet3 nothing will happen. Put the VLOOKUP formula in the code I've written into a sheet and see if any matches are returned.

If you step through the code (by pressing F8) it may give you a better idea of what's happening though as you've been able to get Fluff's nifty code to work that's great.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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