Excel 查找变量并存储值
我需要找到一个文本字符串,并将项目名称存储在文本字符串下面,以便将其放在工作表中的其他位置 示例我想找到“Description”并存储它下面的所有项目,以便稍后在宏中使用 例如,将它们放在B1中 这是我试图使用的代码,但我不知道如何存储活动范围Excel 查找变量并存储值,excel,vba,Excel,Vba,我需要找到一个文本字符串,并将项目名称存储在文本字符串下面,以便将其放在工作表中的其他位置 示例我想找到“Description”并存储它下面的所有项目,以便稍后在宏中使用 例如,将它们放在B1中 这是我试图使用的代码,但我不知道如何存储活动范围 Sub test() 'find description Cells.Find(What:="Description", After:=ActiveCell, LookIn:=xlValues, _ LookAt:=xlWhole, Sear
Sub test()
'find description
Cells.Find(What:="Description", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Offset(1, 0).Select 'Offset after find
Range(Selection, Selection.End(xlDown)).Select 'Selects to end
Dim DescriptionValues As Range
DescriptionValues = Active.Range
ActiveSheet.Range("B10") = DescriptionValues 'put stored text starting in B1
End Sub
当你说“…以后在宏中使用”时,你以后打算如何处理它?一个选项是将所有值存储在一个数组中,以后可以使用。这样做的好处是不必将其实际放置在工作表上,而是将其存储在变量中。它也快一点。现在,请尝试执行
ActiveSheet.Range(“B1:B10”).Value=DescriptionValues.Value
(我不使用Excel,但这可能有用)@BruceWayne将其与其他7个存储值范围一起粘贴到不同的位置。
Sub test()
Dim rng As Range
Set rng = ActiveSheet.Cells.Find(What:="Description", After:=ActiveSheet.Range("A1"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
Set rng = ActiveSheet.Range(rng.Offset(1, 0), rng.End(xlDown))
ActiveSheet.Range("B1").Resize(rng.Rows, 1).Value = rng.Value 'put stored text starting in B1
End If
End Sub