2017-06-07 84 views
-1

我对VBA很陌生。我尝试录制我的宏,但似乎无法解决它。查找并用单元格号替换当前单元格的值

我想宏观到以下几点:

选定的蜂窝小区我要搜索的价值和更换什么是选定单元格“号”发现。

示例:说我选择单元格A1,其值为666我希望它搜索并替换表单中包含666的所有单元格,其中“= A1”。

实质上,我希望他们都指向最初的单元格。

感谢您的任何帮助。

回答

0

基本上,您循环遍历当前工作表中使用范围内的单元格并替换单元格值。下面是将做你所追求的代码。我评论了每一行,以明确究竟发生了什么。

Sub Test() 
'Range variable to use for looping 
Dim rng As Range 

'String variable for finding the values. 
Dim searchvalue As String 

'We'll loop on the current sheet 
With ActiveSheet 

    'You need to have a single cell selected, if that's not the case we exit the macro. 
    If Selection.Count > 1 Then Exit Sub 

    'Get the value to search for 
    searchvalue = Selection.value 

    'Loop over all cells in the sheet that have values. 
    For Each rng In .UsedRange 

     'We do not want to overwrite the range from where we search (the selection) 
     If Intersect(rng, Selection) Is Nothing Then 

      'And if we find the value that was in the selection 
      If rng.value = searchvalue Then 

       Then create the =cell we selected formula. 
       rng.value = "=" & Selection.Address ' 
      End If 
     End If 

    'Next cell in the loop 
    Next rng 

'And we don't need the activesheet anymore. 
End With 
End Sub 

由于这是非常通用的东西,我建议你阅读一般的Excel VBA编程。一些重要的资源是:

+0

通用与否,这不是那么简单掌握的门外汉。尽管如此,非常感谢你,它已经完成了我所需要的。留言Merci! – Phil

相关问题