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
嗨,我正在尝试将单元格值存储在变量中。但是不知何故,它一直给我一个编译错误。 “所需对象”
在我看来,我要告诉变量以字符串表示方面,并且单元格包含一个字符串,如调试器所说。
你能帮我吗?
这里的currentRange是'A7',上面的单元格包含一个带有'200m men'的字符串
错误发生在
Set genderAndDiscipline = ActiveCell.Offset(-1, 0).Value
最佳答案
genderAndDiscipline
声明为字符串。
分配给字符串的正确方法是使用Let
而不是Set
(用于分配对象)。
为了消除错误,请从导致错误的行中删除单词Set
,或将Set
替换为Let
。
即,使用以下两种替代方法之一(等效):
genderAndDiscipline = ActiveCell.Offset(-1, 0).Value
要么
Let genderAndDiscipline = ActiveCell.Offset(-1, 0).Value
关于excel - VBA : Store value to variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792821/