Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在公式中提供单元格地址作为变量_Vba_Excel - Fatal编程技术网

Vba 在公式中提供单元格地址作为变量

Vba 在公式中提供单元格地址作为变量,vba,excel,Vba,Excel,我正在尝试使用VBA从数据转储中提取信息。我的方法是找到触发器字符串并从搜索结果激活的单元格中提取信息 Sub ExtractingValues_T1() Dim ValueLoc As String Sheets("Sheet1").Select 'This is to select the right sheet Range("A1").Select 'This is so that when I perform my find, I get results top down Cells

我正在尝试使用VBA从数据转储中提取信息。我的方法是找到触发器字符串并从搜索结果激活的单元格中提取信息

Sub ExtractingValues_T1()
Dim ValueLoc As String

Sheets("Sheet1").Select 'This is to select the right sheet
Range("A1").Select 'This is so that when I perform my find, I get results top down

Cells.Find(What:="037 = 2001", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate 'This simply selects the first cell that meets my search requirement

'Below is where I run into trouble
ValueLoc = ActiveCell.Address 'Here, I'm trying to get the address of the cell I need to use a formula on
Sheets("Sheet2").Range("C2").Formula = "=RIGHT(ActiveCell, 10)" 'Here I try to use that address in the formula

但是,我犯了一个错误。我理解我的问题在于我的方法,我不能以我这样做的方式提供单元格引用作为变量。但正确的方法是什么

不如像下面这样稍微改变一下,你应该尽量不要使用Select&Activate方法,因为它们很难控制,你可能会得到不想要的结果,下面的代码将实现你所期望的,而不选择任何内容:

Sub ExtractingValues_T1()
Dim foundvalue As Range
'below specified Sheet and Range to look for the value, in this case looking in Column A, amend as required
Set foundvalue = Sheets("Sheet1").Range("A:A").Find(What:="037 = 2001", After:=Sheets("Sheet1").Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not foundvalue Is Nothing Then 'if you found something
    Sheets("Sheet2").Range("C2").Formula = "=RIGHT(" & foundvalue.Address & ", 10)" 'add the address of the found value
End If

End Sub

对formula=“=A”&i&“+B”&i允许您使用由其他变量确定的变量i。。。我可以从一个循环,绑到一个文本框等,谢谢。我不得不做一些小改动,使之符合我的计划,但效果很好。谢谢你的帮助!是的,尽量不依赖于选择和激活是我努力的方向。祝我好运:)@MufaddalM,如果有帮助的话,你能把我的回答记下来吗?谢谢我相信你不需要那么多运气,因为避免使用Select And Activate(选择并激活)并不是那么困难…:)