Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 December 10th, 2006, 09:32 PM
Registered User
 
Join Date: Dec 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Match function retrieve cell reference not

 If I use the "application.match" function to search for a string on two sheets. Is there someway to reference the cell not the XLvalue?.
That is, retrieve and store the cell location for a find function so that I can use that cell reference latter, to copy a value adjacent to it? Range 1 has unique cell values running horizontally. Range 2 runs vertically and can have duplicate values.

'sheet 2 tracking sheet
Sheet2.Visible = True
Set rng2 = Sheet2.Range("A1:A209") 'row and column vertically

'sheet 3 is where rng1 is found
Sheet3.Visible = True 'summary sheet(3)
k = 51 'starting row for copying too, on summary sheet
Set rng1 = Sheet3.Range("A4:HQ4") 'row and column horizontally

 'set up the search
    For Each cell In rng2

    res = "" 'res = some string on sheet(2) in column 1
    res = Application.WorksheetFunction.Match(cell.Value, rng1, 0) ' match string on sheet 2 with something on sht 3

        If Not IsError(res) Then ' they match
            ' do something like start copying cell information
            z = "" 'basically z = res
            z = cell.Value 'set object from Match function
            MsgBox cell.Value & " equals " & rng1(res).Value, vbOKOnly 'check it's working

I've found the matching cells, now if I use .find in this instance its OK-

Set rng7 = Sheets(3).Range("A4:HQ4").Find(what:=z, LookIn:=xlValues) 'find the correct cell
            'using find will only ever find the first cell where the information is found but this is ok as the row values are unique.
Set rng8 = rng7.Offset(k, 1)

as there are only unique values in on the sheet .finf works OK.

Now if there are a number of cells with the same value on the next sheet .find doesn't work - I'd like this time to use a cell reference so that that cell can be used and not the first cell

say we have a1=101 b1=$7, a2=101 b2=$8, a3=109 b3=$52, a4=101 b4=$10,
The first column is accounting codes the second(B) is the value.
Match finds A1=101 and matches it with 101 on the second sheet.
But if I want to use a4, if I use find it will only stop at the first cell A1 but it is A4 I want - hence the need for a cell reference not xlvalue.

Possible?
Or is there another way to locate cells on different sheets and then use that cell reference to latter copy a value from (an adjacent cell) on the first sheet to the free cell that matches adjacent to the value on the second?

VB is needed. The code would reduce an 11meg sheet to around 383kb.
The original uses vlookup and if statements in each cell to copy data from sheet 3 to sheet 2.
Sheet 3 has infromation pasted to it from another workbook, the accounting codes are constant.
The idea is to use the code by clicking a command button once the new data has been added. Sheet 2 is a summary sheet and basically summs different accounting codes.
Everything in a simple -I hope - description.

Thanks
Bill
 
Old December 11th, 2006, 04:35 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Bill

You want to get the address of the cell that has your find value, right!

use rng.Address

Or

If you are specific in row or column

you can use

rng.row / rng.column

Where rng will be your find

http://www.vbadud.blogspot.com
 
Old December 11th, 2006, 05:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Bill,

I must admit I don't fully understand the problem and what you're trying to achieve. I can't help but feel that you are best off using a SUMIF function on Sheet 2 to summarise the values by accounting codes on Sheet 3, however I may be misunderstanding your requirements.

In answer to you specific query - you can grab the cell range variable from the output of a Application.WorksheetFunction.Match function by understanding that the match function returns the position in a range list. Therefore, given your rng1 variable is a row of cells, you should be able to determine the found cell variable by using:

Set rngFound = rng1.Cells(1,res)

For info, you are correct that Find will only return the first found instance (as indeed will the MATCH worksheet function) however you can combine it with the FindNext method to find all instances of a particular search value. See http://p2p.wrox.com/topic.asp?TOPIC_ID=53253 for an example of how to implement this.

Do let us know if I can help any more or any of this does not make sense.
Maccas






Similar Threads
Thread Thread Starter Forum Replies Last Post
variable used as cell reference? dabith Excel VBA 3 March 8th, 2014 12:53 AM
Using a variable to reference a cell MikeCt203 Excel VBA 2 March 24th, 2008 04:02 PM
Reference a Cell in a Webtable. Ahrenl Excel VBA 2 May 25th, 2005 11:25 AM
Passing the cell reference to a variable Artist Excel VBA 0 April 14th, 2004 04:34 AM





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