tags:

views:

441

answers:

3

Hi,

I'm trying to find a command which returns the index of the row for which a particular value occurs. In addition, the value cannot be empty.

In total, there are 3 worksheets. Worksheet A has a column with all the values. Worksheet B has a column with values which appear in worksheet A and columns with more information for that value and I want to copy that information into worksheet C.

Say worksheet A is (lines represent empty cells):

a
b
c

Worksheet B looks like this before I run the macro:

a  12  32  
c  34  45
b  23  21

Worksheet C looks like this before I run the macro:

a
b
c

and like this after I run the macro:

a  12  32  
b  23  21
c  34  45

The structure of the macro looks like this:

  • If the value in worksheet A is not empty, look for a row that corresponds to the value in worksheet "B".
  • Copy the information in the row from worksheet B.
  • Paste it into row i in worksheet "C".
A: 

Have you looked at Excel's VLookup function? You can use that to find a value in a sheet and then get another value from that row. each of your cells in C could use a VLookup to get the correct value from B (or blank if it does not exist).

Jeff Storey
A: 

yea , I allready tried it with VLOOKUP , but I only managed to the find a value from an array not the index of the row.

ie =VLOOKUP(1,A2:C10,2)

excel34
Why do you need the row index? If worksheet C A1:A3 have a, b, c, then worksheet C B1:B3 would be something like VLOOKUP(A1,wkshtB!a1:wkshtB!c3,2) and then C1:C3 would just shift the index to 3. I'm not sure if VLookup will allow you to reference another sheet, but if you can't, copy the values into another cell in the current sheet and hide those columns.
Jeff Storey
A: 

Try this

Public Sub ProcessData()
Dim i As Long
Dim LastRow As Long
Dim RowNum As Long

    With Worksheets("C")

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 1 To LastRow

            If Not IsError(Application.Match(.Cells(i, "A"), Worksheets("A").Columns(1), 0)) Then

                RowNum = Application.Match(.Cells(i, "A"), Worksheets("B").Columns(1), 0)
                Worksheets("B").Cells(RowNum, "B").Resize(, 2).Copy .Cells(i, "B")
            End If
        Next i
    End With

End Sub
Bob Phillips