0

How do I retrieve the cell next to the one that has been defined? Cell "A2" stores AT and I want to display Austria which i have displayed in cell "B2" instead of AT. I need this functionality in a For loop.

AT  Austria
IT  Italy
FR  France

I need to do the For loop using AT (for other purposes) but i want to write away the country name (Austria or Italy etc)

The macro below gives run-time error 424 Object required.

Sub test()
    Dim country
    Dim country_list
    Dim counter

    country_list = Worksheets("Sheet1").Range("A2:A4")
    counter = 1

    For Each country In country_list
        Worksheets("Sheet2").Cells(counter, 1).Value = country.Offset(0, 1).Value
        counter = counter + 1
    Next country
End Sub

2 Answers 2

1

You just need to use set for this to work the way you want.

Set country_list = Worksheets("Sheet1").Range("A2:A4")

It returns error sine your variable was not assigned an object.

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

Comments

0
Worksheets("Sheet1").Range("A2").Offset(0,1).Value

As it is right now, you're not passing a valid range to the Range(country)... method. You have defined country to be the value of the cell "A2".

The 1004 error means that you are attempting to access a range by address which either does not exist, or cannot be accessed. In this case, the range simply doesn't exist.

4 Comments

thanks David, that helps. However, i still want to make the reference to country, as in the For loop i cannot say "A2" because it can also be "A3" etc
so, what do i need to do for the following to work: Country.Offset(0,1).Value
You need to show more of your code. Country must be a valid Range object at a minimum.
thanks David... i changed my Question (providing more code) and i got the answer in the meantime. thanks for your help!

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.