Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2008, 05:35 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Matching Values between two ranges

Hi all,

I need help matching values between two ranges. I can't seem to figure out how to utilize VLOOKUP, FIND, or MATCH to suit my needs. With the following code I have set my search range to a list. I simply took a range in Sheet2 and converted it to a list which probably isn't necessary. At any rate, I now need to be able to search CloumnC in Sheet1 for matching values in this list and when found, populate the corresponding cell (in the same row of the found value) in ColumnE on Sheet1 with a value of "Late". The code below identifies the search range and converts it to a list. How do I implement VLOOKUP, MATCH or FIND or any other method for that matter to accomplish this.

Sub GetStatus()
Dim intlastrow As Integer
Dim i As Integer
Dim rng As Range
Dim cel As Range
Dim Stat As String
Dim newrng As String
Dim v As Variant
With Sheets(2).Activate
intlastrow = ActiveCell.CurrentRegion.Rows.Count
ActiveCell.Offset(intlastrow, 0).Select
Range("A2").Select

Set rng = Range("A2:A" & intlastrow)

'For i = 1 To intlastrow
For Each cel In rng.Cells
'Stat = Range("A2").Value
'MsgBox Stat
'MyForm.ListBox1.AddItem cel.Value
Stat = cel.Value
Sheets(1).Activate
newrng = Application.Match(Stat, Range("C2:C100"), 0) ' this fails
MsgBox newrng
'v = VLookup(Stat, newrng, 3, False)' I couldn't get this to compile
Next

'MyForm.Show
Sheets(1).Activate
End With
''vlookup
End Sub
 
Old May 1st, 2008, 09:20 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Dear Hood

I think the post is duplicate!

Anyways, here is your possible solution

Code:
Sub GetStatus()
Dim intlastrow As Integer
Dim i As Integer
Dim rng As Range
Dim cel As Range
Dim Stat As String
Dim newrng As Range
Dim v As Variant

intlastrow = Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Row

'Set rng = Sheets(2).Range("A2:A" & intlastrow)

For i1 = 1 To intlastrow
    Stat = Trim(Sheets(2).Cells(i1, 1).Value)
    If Len(Stat) <> 0 Then
        Set newrng = Sheets(1).Range("C:C").Find(Stat)
        If Not newrng Is Nothing Then
            ' If match is found, value of column E will be Late
            Sheets(2).Cells(i1, 5).Value = "late"
        End If
    End If
Next

End Sub
Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 2nd, 2008, 09:55 AM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by hood8jmark
 Hi all,

I need help matching values between two ranges. I can't seem to figure out how to utilize VLOOKUP, FIND, or MATCH to suit my needs. With the following code I have set my search range to a list. I simply took a range in Sheet2 and converted it to a list which probably isn't necessary. At any rate, I now need to be able to search CloumnC in Sheet1 for matching values in this list and when found, populate the corresponding cell (in the same row of the found value) in ColumnE on Sheet1 with a value of "Late". The code below identifies the search range and converts it to a list. How do I implement VLOOKUP, MATCH or FIND or any other method for that matter to accomplish this.

Sub GetStatus()
Dim intlastrow As Integer
Dim i As Integer
Dim rng As Range
Dim cel As Range
Dim Stat As String
Dim newrng As String
Dim v As Variant
With Sheets(2).Activate
intlastrow = ActiveCell.CurrentRegion.Rows.Count
ActiveCell.Offset(intlastrow, 0).Select
Range("A2").Select

Set rng = Range("A2:A" & intlastrow)

'For i = 1 To intlastrow
For Each cel In rng.Cells
'Stat = Range("A2").Value
'MsgBox Stat
'MyForm.ListBox1.AddItem cel.Value
Stat = cel.Value
Sheets(1).Activate
newrng = Application.Match(Stat, Range("C2:C100"), 0) ' this fails
MsgBox newrng
'v = VLookup(Stat, newrng, 3, False)' I couldn't get this to compile
Next

'MyForm.Show
Sheets(1).Activate
End With
''vlookup
End Sub

Thanks Shasur. That did the trick. I appreciate it.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching values in Sheet1 to those in other sheet hood8jmark Excel VBA 1 May 1st, 2008 09:07 AM
two workbooks--matching values within kwik10z Excel VBA 0 November 29th, 2007 11:51 AM
Date Ranges nbuckwheat Access 1 December 12th, 2005 12:45 PM
date ranges yuqlin BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 2 December 23rd, 2004 05:23 PM
Matching of attribute values gb XSLT 2 February 10th, 2004 04:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.