0
Sub test() ' ' test Macro '

    Dim aRange As Range
    Dim i As Integer
    
    Set aRange = Range("A1:A255")
    
    Range("A1").Select
    For i = 1 To aRange.Count - 1
    
        If InStr(ActiveCell.Value, "Last name") Then
            Call CopyContents
        End If
        ActiveCell.Offset(1, 0).Select 
        
    Next i
    
End Sub


Sub CopyContents()
    Dim currentRange As Range
    Dim genderAndDiscipline As String

    Set currentRange = Range(ActiveCell.Address)
    
    'get the gender and dicipline
    Set genderAndDiscipline = ActiveCell.Offset(-1, 0).Value
    'genderAndDiscipline = genderAndDiscipline.Split(" ")
    
End Sub

I'm trying to store a cell value in a variable. But somehow it's keep giving me an compile error:

Object required

In my opinion, I'm telling the variable to aspect a string and the cell is containing a string, as the debugger says.

The currentRange is 'A7' here and the cell above is containing a string with '200m men'

The error occurs at
Set genderAndDiscipline = ActiveCell.Offset(-1, 0).Value

2
  • Where does the error occur? Commented May 28, 2013 at 13:03
  • Try removing the Set keyword from that line Commented May 28, 2013 at 13:09

3 Answers 3

4

genderAndDiscipline is declared as a string.

The correct way to assign to a string is using Let instead of Set (which is used for assigning objects).

In order to get rid of the error, remove the word Set from the line causing the error, or replace Set with Let.

That is, use one of the following two alternatives (which are equivalent):

genderAndDiscipline = ActiveCell.Offset(-1, 0).Value

or

Let genderAndDiscipline = ActiveCell.Offset(-1, 0).Value
Sign up to request clarification or add additional context in comments.

Comments

2

The Set keyword works with Objects. Since you are looking just to save the value, you should leave this out. i.e. Change:

Set genderAndDiscipline = ActiveCell.Offset(-1, 0).Value

to:

genderAndDiscipline = ActiveCell.Offset(-1, 0).Value

Comments

1

Remove Set from Set genderAndDiscipline = ActiveCell.Offset(-1, 0).Value

Comments

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.