0

I am trying to use relative reference in a loop. My aim is to use index and match combined to find values independent of how many rows and columns are present in the file. This is what I have created so far, but it does not work. The data is the the same stored on two different sheets.

 Error msg: Run-time error '1004':

Unable to get the Match Property of the WorksheetFunction class

Sub testing()

    Dim ActSor As Long, ActOsz As Long

    ActSor = ActiveSheet.UsedRange.Rows.Count 
    ActOsz = ActiveSheet.UsedRange.Columns.Count

    Dim ws1 As Worksheet, ws2 As Worksheet

    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")

    Dim Rg As Range

    Set Rg = ws2.Range("B2", Cells(ActSor, ActOsz))

    Dim RgActOsz As Long

    RgActOsz = Rg.Columns.Count

    Dim i As Long , sor As Long

    For i = 2 To RgActOsz
       For sor = 2 To ActSor
          Cells(sor, i).Value = Application.WorksheetFunction.Index(ws1.Columns(i),
          Application.WorksheetFunction.Match(Cells(sor, 1), Rg.Columns(1), 0))
       Next sor
    Next i

End Sub
6
  • 2
    you need to trap errors in case you will not have a successful Match Commented Dec 27, 2016 at 15:20
  • Thanks for the help. The 2 sheets are identical and it is only created for testing purposes, which means that all values can be found on both sheets. Do I need to trap errors in this case as well? Commented Dec 27, 2016 at 15:26
  • not really, just if you have some logic error somewhere in your code. In my code I trap it anyway, in case I have an extra space at the end of the text, or some other fault I might have done Commented Dec 27, 2016 at 15:28
  • Why are doing Cells.Value instead of just Cells = ? You're getting the error because the match is failing though. Commented Dec 27, 2016 at 15:28
  • Thanks for the help. I have removed the .value and still getting the same error msg. Commented Dec 27, 2016 at 15:45

2 Answers 2

1
Set Rg = ws2.Range("B2", Cells(ActSor, ActOsz))

this is forbidden. Correct syntax:

Set Rg = Range( ws2.Range("B2"), ws2.Cells(ActSor, ActOsz))

Does that help?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. Nope, still the same err msg.
0

Any time you use a Range() object there is an implicit ActiveSheet.Range() command used. IF the sheet you want to pull values from is not active the Range() command will give the wrong results.

My suggestions:

  1. Qualify all Range() functions with the appropriate worksheet object, like ws1.Range().
  2. Pick the top left cell in a table with a range, and then extend to the table using the Resize() command. For example to pick cell A2 and the next 100 rows and 20 columns, use ws1.Range("A2").Resize(100,20).
  3. Instead of using Cells() which also have an implicit active worksheet use an existing range and use the Offset() command. To pick the i-th row of a table with 20 columns starting from cell A2 use Range("A2").Offset(i-1,0).Resize(1,20)
  4. Use named ranges to pick cell references because they move with the cells. For example Range("AccountsTable").Offset(i-1,0).Resize(1,20)

3 Comments

Thanks for the help @Ja72 The users can modify the original sheet - can add more columns - that is why I would not like to use the offset or the resize bacause that way I would need to modify the code each time. Is it possible to write a loop code that uses vloopup or index&match - I know that errror trapping is still missing - where it checked the value in column A and then brings back the values of the column, and if there are 10 columns it brings back the 10th column value and if there are 100, then the 100th?
The way to deal with this it to use Named Ranges such that you never reference absolute locations, but always relative. I will edit my answer.
I will see if I can resolve it this way. Thanks for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.