0

I have a table in Excel, and in VBA this bit of code:

If TableName(Y, 1) = "Dog" Then

Which works fine, it looks up the value of the first column and checks if it = "Animal" But instead of using (Y, 1), I want to do it by declaring the column header instead of column number, eg: (Y, [AnimalHeader]), but this doesn't work, does anyone know is this is possible and how?

11
  • what about named ranges? Commented Mar 28, 2017 at 15:50
  • Yeh that could be one way, but Im surprised I can just reference to the header name like I can within the spreedsheet Commented Mar 28, 2017 at 15:52
  • Sorry, it's probably not what you want since you are using a table. However, what about a function to fetch the column, so that way you can use (Y, fetchColumn(Animal)) Commented Mar 28, 2017 at 15:57
  • 2
    is TableName the name of a ListObject variable or what? Commented Mar 28, 2017 at 16:00
  • 1
    And what is Y? Some more context please. Commented Mar 28, 2017 at 16:02

1 Answer 1

2

You can't get there quite that directly, I don't think, but this is close:

Dim lo as Excel.ListObject

Set lo =  Ws.ListObjects("TableName")
If lo.ListColumns("Animal").DataBodyRange(Y) = "Dog" Then ...
Sign up to request clarification or add additional context in comments.

1 Comment

This was pretty much exactly what I was looking for, thanks!

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.